亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從 WC_Subscription 實例對象獲取產品 ID

如何從 WC_Subscription 實例對象獲取產品 ID

PHP
料青山看我應如是 2023-08-19 16:20:34
這用于完成初始訂閱付款和訂閱續訂。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");
查看完整描述

1 回答

?
千巷貓影

TA貢獻1829條經驗 獲得超7個贊

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

}

經過測試并有效。

查看完整回答
反對 回復 2023-08-19
  • 1 回答
  • 0 關注
  • 139 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號