对于 WooCommerce 订单上具有完成状态的特定产品,执行操作
For specific products on WooCommerce orders with completed status, perform an action
在 WooCommerce 中,如果购买了列表中的至少一种产品并且该产品的当前订单状态已完成,我想执行一个操作。
比如我只能验证是否购买了产品:
global $woocommerce;
$user_id = get_current_user_id();
$current_user= wp_get_current_user();
$product_list = array('11', '12', '13', '14', '15','16');
$text= false;
foreach ($product_list as $value):
if (wc_customer_bought_product( $customer_email, $user_id, $value) ) {
$text = true;
}
endforeach;
尝试每次订单获得“完成”状态时触发的以下操作,检查当前订单是否包含您定义的 ID 中的特定产品,从而允许您执行操作:
add_action('woocommerce_order_status_completed', 'action_on_order_status_completed', 10, 2 );
function action_on_order_status_completed( $order_id, $order ){
// Here below define your product id(s)
$products_ids = array('11', '12', '13', '14', '15','16');
$found = false; // Initializing
// Loop through order items
foreach ( $order->get_items() as $item ) {
if ( array_intersect([$item->get_product_id(), $item->get_variation_id()], $products_ids) ) {
$found = true;
break; // Stop the loop
}
}
if ( $found ) {
// HERE do your action
}
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。测试和工作。
相关:
在 WooCommerce 中,如果购买了列表中的至少一种产品并且该产品的当前订单状态已完成,我想执行一个操作。
比如我只能验证是否购买了产品:
global $woocommerce;
$user_id = get_current_user_id();
$current_user= wp_get_current_user();
$product_list = array('11', '12', '13', '14', '15','16');
$text= false;
foreach ($product_list as $value):
if (wc_customer_bought_product( $customer_email, $user_id, $value) ) {
$text = true;
}
endforeach;
尝试每次订单获得“完成”状态时触发的以下操作,检查当前订单是否包含您定义的 ID 中的特定产品,从而允许您执行操作:
add_action('woocommerce_order_status_completed', 'action_on_order_status_completed', 10, 2 );
function action_on_order_status_completed( $order_id, $order ){
// Here below define your product id(s)
$products_ids = array('11', '12', '13', '14', '15','16');
$found = false; // Initializing
// Loop through order items
foreach ( $order->get_items() as $item ) {
if ( array_intersect([$item->get_product_id(), $item->get_variation_id()], $products_ids) ) {
$found = true;
break; // Stop the loop
}
}
if ( $found ) {
// HERE do your action
}
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。测试和工作。
相关: