WooCommerce:从订阅订单中获取名称和变体
WooCommerce: Get name and variation from a Subscription order
我想在我的有效订阅概览中显示订阅产品的名称。目前页面只显示订阅订单号,函数为$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…
相关官方开发者文档:
我想在我的有效订阅概览中显示订阅产品的名称。目前页面只显示订阅订单号,函数为$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…
相关官方开发者文档: