要更新订单 ID 数组的订单状态,您可以使用多种方式(但始终在之前备份数据库,或者至少备份 wp_posts 表)。
注意: Woocommerce 订单 post_status 始终以 wc- 开头。
1) 最好的方法是使用WPDB WordPress 类的轻量级、非常高效且唯一的 SQL 查询,这样:
global $wpdb;
$new_status = 'wc-custom-status';
$product_ids = array(37, 53, 57, 63, 80); // The array of product Ids
$product_ids = implode(',', $product_ids);
$wpdb->query( "
UPDATE {$wpdb->prefix}posts
SET post_status = '$replacement_user_id'
WHERE ID IN ($product_ids)
AND post_type = 'shop_order'
" );
2)另一种方法 (较重)是更新订单ID数组的状态是在foreach循环中使用WordPress wp_update_post()函数,这种方式:
$new_status = 'wc-custom-status';
$product_ids = array(37, 53, 57, 63, 80); // The array of product Ids
$product_ids = implode(',', $product_ids);
foreach ( $orders_ids as $order_id ) {
wp_update_post( array('ID' => $order_id, 'post_status' => $new_status) );
}
这两个代码都经过测试并且可以正常工作。
您可以将代码嵌入到函数中并通过挂钩(甚至使用简码)触发它。
我不建议你使用WC_Product方法update_status(),因为它会很重(它会向客户发送通知,以获取特定的订单状态)