To update the order status of an array of order Ids, you can use many ways (but always make a database backup before, or at least the wp_posts
table).
Note: Woocommerce order post_status
start always by wc-
.
1) The best way is to use a lightweight, very efficient and unique SQL query with the WPDB
WordPress Class, this way :
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) Another way (heavier) is to update the status of an array of order Ids is to use WordPress wp_update_post()
function in a foreach loop, this way:
$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) );
}
Both codes are tested and works.
You can embed the code in a function and trigger it from a hook (or even using a shortcode).
I not recommend you to use the WC_Product
method update_status()
, as it will be very heavy (and it will send a notification to the customer, for specific order statuses)