Woocommerce Subscriptions - Get Start date

余生颓废 提交于 2020-01-15 06:51:53

问题


I'm trying to create a function that returns the start date of a users subscription. I'm using woocommerce and the subscriptions plugin.

I had this code working, however, it returned a "depreciated" error in my debug.log - and was also VERY slow loading:

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;
}

After a bit of searching around, I came across this function in the documentation:

WC_Subscription::get_date( 'start' );

But this also gives me a number of errors as follows:

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

Is there a simple way to grab the subscription start date that I'm missing?

UPDATE: When using 'date_created' instead of 'start' I get the following errors:

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

回答1:


You must loop over array of subscriptions:

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

and get a subscription object like this:

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

Only then you will be able to get dates via function, for example:

$subscription->get_date( 'start' );



来源:https://stackoverflow.com/questions/45280397/woocommerce-subscriptions-get-start-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!