I need to change the order status from the default ‘processing’ status to ‘priority’ if a customer chooses any 1 of 4 express shipping options.
I’m using the classic checkout.
Here’s what I’ve tried so far but nothing happens when a user places an order.
add_action( 'woocommerce_thankyou', 'shipping_method_update_order_status', 10, 1 );
function shipping_method_update_order_status( $order_id ) {
if ( ! $order_id ) return;
// Here define your shipping methods Ids
$shipping_method_0_flat_rate25 = array('flat_rate');
$shipping_method_0_flat_rate26 = array('flat_rate');
$shipping_method_0_flat_rate27 = array('flat_rate');
$shipping_method_0_flat_rate28 = array('flat_rate');
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
// Get the WC_Order_Item_Shipping object data
foreach($order->get_shipping_methods() as $shipping_item ){
// For testing to check the shipping method slug (uncomment the line below):
// echo '<pre>'. print_r( $shipping_item->get_method_id(), true ) . '</pre>';
if( in_array( $shipping_item->get_method_id(), $shipping_method_0_flat_rate25 ) && ! $order->has_status('priority_dispatch') ){
$order->update_status('priority_dispatch'); // Already use internally save() method
break; // stop the loop
}
elseif( in_array( $shipping_item->get_method_id(), $shipping_method_0_flat_rate26 ) && ! $order->has_status('priority_dispatch') ){
$order->update_status('priority_dispatch'); // Already use internally save() method
break; // stop the loop
}
elseif( in_array( $shipping_item->get_method_id(), $shipping_method_0_flat_rate27 ) && ! $order->has_status('priority_dispatch') ){
$order->update_status('priority_dispatch'); // Already use internally save() method
break; // stop the loop
}
elseif( in_array( $shipping_item->get_method_id(), $shipping_method_0_flat_rate28 ) && ! $order->has_status('priority_dispatch') ){
$order->update_status('priority_dispatch'); // Already use internally save() method
break; // stop the loop
}
}
}
2
Answers
Key Changes:
1.Simplified Shipping Method IDs: Combined all express shipping methods into a single array.
2.Correct Method ID Format: Ensure the method IDs match the format returned by get_method_id(), which typically includes the method type and instance ID (e.g., flat_rate:25).
3.Status Update: Changed the status to ‘priority’ to match your requirement.
It is better to avoid using
woocommerce_thankyou
hook to change the order status from processing orders for many reasons. The hookwoocommerce_order_status_processing
is more convenient and reliable, as it’s triggered when an order gets the "processing" status.Also, you can use instead the unique shipping method instance ID (which is the number after the
:
character, from your slugs).Caution: Be sure that the slug for your custom order status is "priority_dispatch".
Try the following revised code:
It should work.
Related: Get orders shipping items details in WooCommerce 3
How to get the correct shipping rate ID(s) and instance ID(s)
The following function will display, near the footer (on frontend, when there are items in cart), the available shipping rates IDs and instance IDs:
Once you get the correct shipping rate IDs (or instances IDs) to be used in your code, you can remove this code snippet.