Woocommerce Wordpress
amitm  

Woocommerce: Automatically change order status based on chosen shipping method.

If you are running an e-commerce website using woocommerce, and using multiple shipping methods to ship the orders. You must have felt the requirement to have some easy way to filter orders based on the shipping method chosen by customer during checkout.

Out of the box, there is no way to filter orders based shipping method, but woocommerce does provides ability to filter based on order status. We can leverage this feature using a small trick and few lines of custom php code.

So here is the deal, we can define few custom order statuses (using code, or by using any plugin) in woocommerce, then with few lines of php, we can programmatically move order to a particular custom status based on the chosen shipping method. Sounds like a deal? Lets do it.

add_action( 'woocommerce_thankyou', 'bt_update_status_woocommerce_thankyou', 10, 1 );
add_action( 'woocommerce_thankyou_cod', 'bt_update_status_woocommerce_thankyou', 10, 1 );
function bt_update_status_woocommerce_thankyou($order_id){
	$order = wc_get_order( $order_id );
	$order_statuses = wc_get_order_statuses();
	$shipping_method_to_order_status_map = array(
		"shiprocket"=>"wc-transporting_sr",
		"nimbuspost"=>"wc-transporting_nb",
	);
	foreach($shipping_method_to_order_status_map as $shipping_method=>$order_status) {
	  	if(strcasecmp($order->get_shipping_method(),$shipping_method)==0  && isset($order_statuses[$order_status])){
			if ( $order->has_status( 'processing' ) ) {
				$order->update_status( $order_status);
			}
		}
	}	
}

Just change lines 7-8 as per your requirements. The variable $shipping_method_to_order_status_map holds the mapping of shipping method to order status.

Shipping method names can be found by going to Woocommerce Settings -> Shipping -> Zone Name. Refer this screenshot:

Just copy-paste the “Title” as the key of the $shipping_method_to_order_status_map array in line 6.

For target order status, if you are using custom order status, you can get order status names from the plugin that you might be using for creating order statuses. Refer this screenshot:

Remember to prefix “wc-” to the slug of custom status. As for the one in above screenshot, the status shall be: wc-transporting_sr. So the array will look like this:

$shipping_method_to_order_status_map = array(
		"shiprocket"=>"wc-transporting_sr"
	);

The code can be installed inside functions.php file of your theme or by using a plugin like “Code Snippets”, as shown in this screenshot:

Now, for every new order, the order status will be automatically updated based on what customer chose as shipping during checkout. Here are few examples:

As a bonus, now you can easily filter orders based on shipping method, right from woocommerce dashboard!! See the tiny links below “Add Order” button in above screenshot!! 🙂 🙂

We tested this code snippet on our development server and it appears to be working fine on current version of wordpress and woocommerce.

Has it worked for you? has it not? Let me know in comments!!

0 0 votes
Article Rating
Subscribe
Notify of
guest
2 Comments
oldest
newest most voted
Inline Feedbacks
View all comments
David
1 year ago

It is exactly what I would need, but unfortunately it doesn’t work for me. :/

Abhinav
Abhinav
9 months ago

Its what I need but this doesn’t work for me

2
0
Would love your thoughts, please comment.x
()
x