如何从 WC_Subscription 实例对象中获取产品 ID
How to get the Product ID from a WC_Subscription instance Object
这个用于完成初始订阅付款和订阅续订。
function payment_made($subscription){
// How do I get the Product ID from subscription? (Definitely need this)
}
add_action("woocommerce_subscription_payment_complete", "payment_made");
这一个用于状态更改时,因此我可以处理手动和系统更改,无论是手动覆盖还是 failed/pending/active/whatever 基于付款或切换的状态。
function status_update($subscription, $old_status, $new_status){
// How do I get the Product ID from subscription (Definitely need this)
}
add_action("woocommerce_subscription_status_updated", "status_updated");
要从 WC_Subscription
对象获取产品 ID,您需要使用 get_items()
方法遍历订单项(因为您可以有很多),例如:
$order_items = $subscription->get_items();
// Loop through order items
foreach ( $order_items as $item_id => $item ) {
// Get the WC_Product_Subscription Object
$product = $item->get_product();
// To get the subscription variable product ID and simple subscription product ID
$product_id = $item->get_product_id();
// To get the variation subscription product ID
$variation_id = $item->get_variation_id();
// Or to get the simple subscription or the variation subscription product ID
$_product_id = $product->get_id();
}
已测试并有效。
相关:
这个用于完成初始订阅付款和订阅续订。
function payment_made($subscription){
// How do I get the Product ID from subscription? (Definitely need this)
}
add_action("woocommerce_subscription_payment_complete", "payment_made");
这一个用于状态更改时,因此我可以处理手动和系统更改,无论是手动覆盖还是 failed/pending/active/whatever 基于付款或切换的状态。
function status_update($subscription, $old_status, $new_status){
// How do I get the Product ID from subscription (Definitely need this)
}
add_action("woocommerce_subscription_status_updated", "status_updated");
要从 WC_Subscription
对象获取产品 ID,您需要使用 get_items()
方法遍历订单项(因为您可以有很多),例如:
$order_items = $subscription->get_items();
// Loop through order items
foreach ( $order_items as $item_id => $item ) {
// Get the WC_Product_Subscription Object
$product = $item->get_product();
// To get the subscription variable product ID and simple subscription product ID
$product_id = $item->get_product_id();
// To get the variation subscription product ID
$variation_id = $item->get_variation_id();
// Or to get the simple subscription or the variation subscription product ID
$_product_id = $product->get_id();
}
已测试并有效。
相关: