这用于完成初始订阅付款和订阅续订。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();
}
经过测试并有效。
- 1 回答
- 0 关注
- 104 浏览
添加回答
举报
0/150
提交
取消