我意识到这很可能是一个完全新手的问题,但在这一点上,我感到非常沮丧,我什至不在乎。我正在尝试创建一个具有当前用户订阅开始日期的变量(如果他们有的话),但我无法拉入开始日期。
这就是我到目前为止所拥有的 - 它最终不会是一个短代码(它将是我放入函数中的变量)我只是将其设置为简码,以便我可以看到输出 - 它返回用户存储信息的整个数组。我刚过开始日期:)
function subscriber_start_date(){
$start_date = WC_Subscriptions_Manager::get_users_subscription( $user_id, 'start_date' );
print_r($start_date);
}
add_shortcode("subscriber-start-date","subscriber_start_date");
我已经查看了这里和这里的文档,但仍然没有对此进行任何研究,我知道这将是一件愚蠢的事情,比如在某处添加一个变量、逗号或 [''] - 我已经尝试了无数对我来说有意义的组合,但没有任何东西起作用(这是唯一返回任何有用内容的组合)。
任何有用的评论将不胜感激。提前谢谢。
值得一提的是几件事:
首先,这不是Core WooCommerce,它是一个附加组件(订阅)。
其次,您链接到的文档明确指出:
。每个订阅都作为具有以下值的数组返回...
这意味着该函数不会只返回开始日期。
换句话说,第二个字段是订阅标识符。 一个用户可以有多个订阅。 此函数允许您从订阅中获取特定订阅,而不是特定字段。
因此,为了执行所需的操作,您需要按如下方式添加到代码中:
function subscriber_start_date() {
// NOTE: You don't have the $user_id - are you setting it?
// Somehow you need to identify the subscription you want.
$subscription_id = 'MY_SUBSCRIPTION_ID';
$subscription = WC_Subscriptions_Manager::get_users_subscription( $user_id, $subscription_id );
$start_date = (isset($subscription['start_date'])) ? $subscription['start_date'] : FALSE;
var_dump($start_date);
}
add_shortcode("subscriber-start-date","subscriber_start_date");
如果您不知道订阅 ID,则可以执行以下操作来获取用户的"第一个"订阅:
function subscriber_start_date() {
// Set start date to initial value
$start_date = FALSE;
// Get ALL subscriptions
$subscriptions = WC_Subscriptions_Manager::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;
}