2017-07-24 68 views
1

我正在尝试创建一个返回用户订阅的开始日期的函数。我正在使用woocommerce和订阅插件。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 

是否有一种简单的方法来获取我缺少的订阅开始日期?

UPDATE: 当使用 'DATE_CREATED' 而不是 '开始',我得到了以下错误:

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 
+0

尝试'WC_Subscription :: get_date( 'DATE_CREATED');'而不是 – stevenkellow

+0

@stevenkellow刚刚试了一下,但还是来了(加到我的问题) –

+0

我认为你需要在**之前得到一个**'WC_Subscription' **对象**的实例,像'$ subscription_obj = new WC_Subscription($ subscition_id);'(所以你需要获得订阅ID),然后你可以安全地使用'$ subscription_obj-> get_date('date_created');'...... – LoicTheAztec

回答

1

您必须遍历订阅的数组:

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

,并得到一个订阅对象如下:

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

只有这样,你就可以通过函数获取日期,例如:

$subscription->get_date('start');

相关问题