1

我想在我的活動訂閱概覽中顯示訂閱產品的名稱。目前,該頁面僅顯示具有功能的訂閱訂單號$subscription->get_order_number()WooCommerce:從訂閱訂單獲取名稱和變體

我在$subscription後面打印了數組,但似乎該數組沒有關於訂閱產品或有序變體的信息。

有沒有辦法顯示訂購訂購產品的名稱和變體?

僅顯示訂單號碼對用戶不是很有幫助。

回答

1

A認購(正像WC_Order對象)可以包含一種或多種產品作爲訂單項。所以這就是爲什麼你可以看到這些數據。

所以正如WooCommerce訂單需要通過認購項目重複,以獲得產品名稱:

// Set here your subscription ID (if needed) 
$subscription_id = 319; 

// Get the WC_Subscription object (if needed) 
$subscription = wc_get_order($subscription_id); // Or: new WC_Subscription($subscription_id); 

// Iterating through subscription items 
foreach($subscription->get_items() as $item_id => $product_subscription){ 
    // Get the name 
    $product_name = $product_subscription->get_name(); 
} 

該代碼測試和工程

認購產品是WooCommerce的延伸產品。因此,可用於WC_Product的方法都可用...


相關負責人的開發者文檔: