我目前正在使用此答案线程中的代码来自动完成订单,以便将新的 WooCommerce 预订添加到日历中(它们只有在标记为完成后才会添加)。
这对预订非常有用,但是,我想将其他一些产品排除在自动完成之外。
你知道这是否可能吗?
我目前正在使用此答案线程中的代码来自动完成订单,以便将新的 WooCommerce 预订添加到日历中(它们只有在标记为完成后才会添加)。
这对预订非常有用,但是,我想将其他一些产品排除在自动完成之外。
你知道这是否可能吗?
与进行更轻量级的 SQL 查询相比,遍历订单项是一个更繁重的过程。
这是一个自定义条件函数,它将检查 Woocommerce 订单中的产品 ID(返回 true 或 false):
function order_has_products( $order_id, $product_ids ){
$product_ids = implode( "','", $product_ids );
global $wpdb;
$result = $wpdb->get_var( "
SELECT COUNT(woim.meta_value) FROM {$wpdb->prefix}woocommerce_order_itemmeta as woim
INNER JOIN {$wpdb->prefix}woocommerce_order_items as woi ON woi.order_item_id = woim.order_item_id
WHERE woi.order_id = $order_id AND woim.meta_key IN ('_product_id','_variation_id')
AND woim.meta_value IN ('$product_ids')
" );
return $result > 0 ? true : false;
}
然后你会这样使用它:
add_action('woocommerce_thankyou', 'wc_auto_complete_order', 20, 1);
function wc_auto_complete_order($order_id){
if (!$order_id) return;
// HERE define your Product Ids to exclude in the array
$product_ids = array( 37, 53 );
// Get an instance of the WC_Product object
$order = wc_get_order($order_id);
$found = order_has_products( $order_id, $product_ids );
if ( in_array( $order->get_payment_method(), array('cod', 'cheque', '') ) || $found ) {
return;
}
else {
$order->update_status('completed');
}
}
代码位于您的活动子主题(或活动主题)的 function.php 文件中。测试和工作。