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 关注
- 86 浏览
添加回答
举报