遍历订单项是一个繁重的过程,而不是进行更轻松的 SQL 查询。
这是一个自定义条件函数,它将检查 Woocommerce 订单中的产品 ID(返回真或假):
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 文件中。经过测试并且可以工作。
WooCommerce: Auto complete paid Orders (depending on Payment methods)