Woocommerce订阅-开始日期 [英] Woocommerce Subscriptions - Get Start date

查看:113
本文介绍了Woocommerce订阅-开始日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我使这段代码正常工作,但是,它在我的debug.log中返回了折旧"错误-而且加载速度也很慢:

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?

更新: 当使用"date_created"而不是"start"时,出现以下错误:

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

推荐答案

您必须遍历订阅数组:

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

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

这篇关于Woocommerce订阅-开始日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆