Woocommerce Subscriptions - Get Start date

2019-07-15 06:21发布

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条回答
老娘就宠你
2楼-- · 2019-07-15 07:04

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' );

查看更多
登录 后发表回答