2016-03-21 34 views
2

我意識到這可能完全是一個總的新手問題,但在這一點上我很沮喪,我甚至不在乎。我試圖用當前用戶訂閱開始日期創建一個變量 - 如果他們有一個 - 並且我被困在開始日期。Get Woocommerce訂閱開始日期

這是我到目前爲止 - 它不會是一個簡短的代碼(它將是一個變量,我把它放入一個函數)我剛剛把它設置爲一個簡碼,所以我可以看到輸出 - 並返回用戶存儲信息的整個數組。我剛剛開始日期:)

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

我看了文件herehere但我仍然只是起草沒事就這一點,我知道這將是愚蠢的東西簡單的像添加變量之後我某處,逗號或[''] - 我嘗試過無數對我有意義的事物組合,但沒有任何工作(這是返回任何使用的唯一組合)。

任何有用的意見將不勝感激。提前致謝。

回答

1

值得一提的幾件事情:

首先,這不是核心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; 
} 
+0

感謝非常有幫助,尊重,及時和徹底的迴應!我想我錯過了當我瘋狂地尋找一種方法使其工作 - 它說我可能還沒有得到你提供的正確的結束編碼。 我沒有設置用戶ID,默認情況下,如果沒有值,它會填充當前用戶,我相信?對於我正在編寫的函數,我只是想看看他們是否有任何訂閱(並且他們一次只能有1個訂閱)。 –