我想在我的有效订阅概览中显示订阅产品的名称。目前,该页面仅显示带有函数 $subscription->get_order_number()
我在 $subscription
后面打印了数组,但该数组似乎没有关于订阅产品或订购的变体的信息。
有没有办法显示订购的订阅产品的名称和变体?
只显示订单号,对用户帮助不大。
最佳答案
订阅(就像 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();
}
此代码已经过测试并且可以工作
Subscription products are an extension of WooCommerce products. So all methods available for WC_Product are usable…
相关官方开发者文档:
关于php - WooCommerce:从订阅订单中获取名称和变体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46592897/