1 回答

TA貢獻1831條經驗 獲得超10個贊
那是因為您使用的鉤子并非用于創建的每個訂單,始終使用的鉤子是woocommerce_thankyou.
所以你得到:(通過附加代碼中的注釋進行解釋)
function action_woocommerce_thankyou( $order_id ) {
// Get $order object
$order = wc_get_order( $order_id );
// Loop through order items
foreach ( $order->get_items() as $item_key => $item ) {
// The WC_Product object
$product = wc_get_product( $item['product_id'] );
// Instanceof
if ( $product instanceof WC_Product ) {
// True
if ( $product->get_featured() ) {
// Set featured false
$product->set_featured( false );
// Save
$product->save();
}
}
}
}
add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 );
- 1 回答
- 0 關注
- 105 瀏覽
添加回答
舉報