【问题标题】:WooCommerce: Get name and variation from a Subscription orderWooCommerce:从订阅订单中获取名称和变体
【发布时间】:2018-03-17 12:03:03
【问题描述】:
我想在我的有效订阅概览中显示订阅产品的名称。目前页面只显示订阅订单号,功能为$subscription->get_order_number()
我在$subscription 后面打印了数组,但该数组似乎没有关于订阅产品或订购的变体的信息。
有没有办法显示订购的订阅产品的名称和变体?
只显示订单号,对用户帮助不大。
【问题讨论】:
标签:
php
wordpress
woocommerce
product
woocommerce-subscriptions
【解决方案1】:
订阅(就像 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 可用的所有方法都是可用的……
相关官方开发者文档: