【问题标题】:How to get the Product ID from a WC_Subscription instance Object如何从 WC_Subscription 实例对象中获取产品 ID
【发布时间】:2020-11-12 12:54:36
【问题描述】:

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

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

这个用于状态更改时,因此我可以处理手动和系统更改,无论是手动覆盖还是失败/待定/活动/基于付款或开关的任何状态。

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");

【问题讨论】:

  • 这个答案不行吗?

标签: php wordpress woocommerce product woocommerce-subscriptions


【解决方案1】:

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

经过测试并且有效。

相关:

【讨论】:

  • 你太棒了!谢谢。因为我现在只卖一件,所以甚至没有考虑多件商品。
猜你喜欢
  • 2020-11-12
  • 2020-11-12
  • 1970-01-01
  • 2021-01-01
  • 1970-01-01
  • 2016-04-25
  • 1970-01-01
  • 2010-10-12
  • 2019-07-08
相关资源
最近更新 更多