问题描述
是否有办法从$subscription
中获取$product
?
感谢此帖子,我知道从$subscription
可以得到$order
:
Thanks to this post, I know that from $subscription
I can get $order
:
$order = $subscription->order;
这也是可能的吗?
$product = $subscription->product;
或者:
$product = $order->product;
推荐答案
由于订阅对象是WC_Order
类的扩展,因此可以从订阅"或匹配的订单中获取产品.
It is possible to get the products from either the Subscription or the matching order, as a subscription object is an extension of the WC_Order
class.
$subscription_products = $subscription->get_items();
$order_products = $subscription->order->get_items();
在大多数情况下,如果您提供订阅服务,则这两者将是等效的,或者$order_products
应该至少包含在$subscription_products
中找到的所有产品.
In most situations where you provide subscription services, both of these will be equivalent or $order_products
should at least contain all products found within $subscription_products
.
一个明显的例外是您手动创建订阅时;没有订单将附加到订阅,因此$subscription->order
可能不是有效的WC_Order
对象.
A notable exception is when you create a subscription manually; no order will be attached to the subscription, so $subscription->order
may not be a valid WC_Order
object.
这篇关于WooCommerce订阅-获取特定订阅的产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!