php - Woocommerce 订阅 - 开始日期

标签 php wordpress woocommerce woocommerce-subscriptions

我正在尝试创建一个返回用户订阅的开始日期的函数。我正在使用 woocommerce 和订阅插件。

我让这段代码正常工作,但是,它在我的 debug.log 中返回了一个“折旧”错误 - 并且加载速度也非常慢:

function subscriber_start_date() {
    global $current_user;
    $user = wp_get_current_user();
    // Set start date to initial value
    $start_date = FALSE;
    // Get ALL subscriptions
    $subscriptions = WC_Subscriptions_Manager::get_users_subscriptions( $user->ID );
    $subscriptions = wcs_get_users_subscriptions( $user->ID );
    if ($subscriptions) {
        // Get the first subscription
        $subscription = array_shift($subscriptions);
        // Get the start date, if set
        $start_date = (isset($subscription['start_date'])) ? $subscription['start_date'] : FALSE;
    }

    return $start_date;
}

经过一番搜索后,我在文档中发现了这个函数:

WC_Subscription::get_date( 'start' );

但这也给了我一些错误,如下所示:

PHP Strict Standards:  Non-static method WC_Subscription::get_date() should not be called statically in /home/skizzar/public_html/wp-content/plugins/lessons-extension/includes/ls-helpers.php on line 28
PHP Notice:  WC_Subscription::get_date was called with an argument that is <strong>deprecated</strong> since version 2.2.0! The &quot;start&quot; date type parameter has been deprecated to align date types with improvements to date APIs in WooCommerce 3.0, specifically the introduction of a new &quot;date_created&quot; API. Use &quot;date_created&quot; in /home/skizzar/public_html/wp-includes/functions.php on line 4023
PHP Fatal error:  Using $this when not in object context in /home/skizzar/public_html/wp-content/plugins/woocommerce-subscriptions/includes/class-wc-subscription.php on line 948

有没有一种简单的方法可以获取我缺少的订阅开始日期?

更新: 当使用“date_created”而不是“start”时,我收到以下错误:

PHP Strict Standards:  Non-static method WC_Subscription::get_date() should not be called statically in /home/skizzar/public_html/wp-content/plugins/lessons-extension/includes/ls-helpers.php on line 28
PHP Fatal error:  Using $this when not in object context in /home/skizzar/public_html/wp-content/plugins/woocommerce-subscriptions/includes/class-wc-subscription.php on line 948

最佳答案

您必须循环订阅数组:

$subscriptions = wcs_get_users_subscriptions( $user->ID ); foreach($subscriptions 作为 $sub)

并获取像这样的订阅对象:

$subscription = wcs_get_subscription( $sub->ID );

只有这样你才能通过函数获取日期,例如:

$subscription->get_date( 'start' );

关于php - Woocommerce 订阅 - 开始日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45280397/

相关文章:

wordpress - Web 托管提供商支持完整的 FTP 访问,但它安全吗?

php - 带有可选参数的路由组

php - "Access denied"与 MySQL 通信时出错

php - 什么是可靠但基本的 php 搜索算法?

php - 更改 Woocommerce 结帐端点以显示订单摘要详细信息

php - 在 Woocommerce 中添加并保存管理产品变体自定义字段

php - 在 BLOB 中存储文本?

聚合匹配项的 mySQL 命令(Drupal 到 Wordpress 的迁移)

php - 如何对 MYSQL 进行多次插入,每次插入都基于子查询中一组值中的每个值?

php - 获取 Woocommerce 单一产品页面中的产品类别名称和描述