如何从 WC_Subscription 实例对象中获取产品 ID [英] How to get the Product ID from a WC_Subscription instance Object

查看:59
本文介绍了如何从 WC_Subscription 实例对象中获取产品 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用于完成初始订阅付款和订阅续订.

This one for completed initial subscription payments and subscription renewals.

function payment_made($subscription){
    // How do I get the Product ID from subscription? (Definitely need this)
}
add_action("woocommerce_subscription_payment_complete", "payment_made");

当状态发生变化时,我可以处理手动和系统更改,无论是手动覆盖还是失败/待处理/活动/基于付款或切换的任何状态.

And this one for when a status is changed, so I can handle manual and system changes either manual overrides or failed/pending/active/whatever status based of payments or switches.

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() 像:

To get the product id from the WC_Subscription Object, you will need to loop through order items (as you can have many) using the method get_items() like:

$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();
}

经过测试并有效.

相关:

这篇关于如何从 WC_Subscription 实例对象中获取产品 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆