fbpx
How to change order status automatically in WooCommerce

How to change order status automatically in WooCommerce

Do you want to update the order status in your store? You’ve come to the right place. In this guide, we’ll show you how to automatically change the order status in WooCommerce using some PHP scripts. Don’t worry if you don’t have advanced coding skills, we’ll explain each script in detail.

WooCommerce offers many features to developers, so if you have coding skills, you can make all kinds of customizations and improve your eCommerce store. In the following tutorial, you will learn all you need to know about the order status. This is a WooCommerce feature that can be very useful when managing a large number of orders. It will be especially useful on the backend orders list, where shop managers can easily search and take certain actions on orders with a specific status.

Without further ado, let’s jump right into it.

What is the order status in WooCommerce?

The order status is simply a tag that an order has that represents the current state of the order. It’s similar to a taxonomy attached to a post that describes specific information about the current state of that post. For example, when a customer hits the “Place order” button, they place an order in our shop. By default, the order status is set to “Pending Payment”.

If the store is integrated with a payment gateway like PayPal or Stripe, the previous default status (pending) will be validated and it will change to “On hold” or “Failed” depending on the payment gateway validation result. Then, if the payment has been completed, the order status will automatically change to “Processing“, while if the payment fails, its status will be “Failed“.

As you can see, each of these statuses gives us relevant information about the order. This automation lets us know if the payment for the order has been completed or not to deliver our product to the customer.

Order Status in WooCommerce

These are all the order statuses available in WooCommerce and the meaning of each one:

  • Pending payment: The order has been received and no payment has been registered. We’re waiting for the payment
  • Failed: The payment has failed for some reason. This means that it was rejected from the payment platform (i.e, PayPal) or that it requires further authentication (SCA)
  • Processing: The payment has been received by the shop, the stock of the product has been updated and the purchase is on schedule for delivery
  • Completed: The order has been fulfilled and completed. This is the last state of an order where everything went well
  • On hold: The customer hasn’t made any payment, so the order requires confirmation from the shop manager
  • Canceled: Either the shop admin or the user has canceled the order at some point. This doesn’t require any further action, although it’s recommended to contact the customer to understand why they’ve canceled the order
  • Refunded: The order has been refunded, no further action is required

Now that we better understand what order status is and its function, let’s see why it can be a good idea to update it.

Why change the default order status function?

Each eCommerce store is different and has specific requirements. Some online stores might not use order status at all or might not use all of them. For example, if shoppers can’t return the products, the store might not use the Refund status.

Additionally, there might be some cases where the default order status feature isn’t enough and the shop needs a custom status or an automated solution for their orders status management. In these cases, customizing the order status behavior is a great solution to improve shop management. This way, the eCommerce store won’t need to update the order status manually in every single case.

This will help store owners save time and let them focus on more important things to improve their businesses.

How the order status changes in WooCommerce

At this point, it’s important to understand that order status changes dynamically when WooCommerce is integrated with a payment gateway like PayPal, Payoneer, Stripe, and so on. Keep in mind that if you update the status manually when it’s not expected, you can break the payment method featured in your store and customers won’t be able to follow the purchase process.

To better understand what we are going to do, let’s have a look at how the order status flow works:

woocommerce-order-status

From WooCommerce documentation

As you can see in the above graphic, the first stage of the order status is “Pending“. Right after that, the payment gateway validates if the user can complete the payment and changes its status to “On hold“, and then to “Processing” when the payment is completed successfully. Otherwise, it will turn to “Failed“.

After this, WooCommerce won’t do anything else. The shop manager should mark the order as “Completed“, or “Cancelled“. At this point, it’s safe to change the order status automatically (programmatically) even if there’s a payment gateway integrated.

Additionally, it’s important to know that when the status changes to “Processing” or “Refunded“, the stock of the products involved in the order will be recalculated. The same will happen with all the statistics of the online store related to that order.

Now that we have a clear idea of how the order status changes, let’s see how to update the order status in WooCommerce.

How to change the order status automatically in WooCommerce

Let’s have a look at some sample scripts to change the order status programmatically in WooCommerce.

NOTE: We’ll edit some core files, so before you start, make sure you do a full backup of your site. Additionally, create a child theme or use any of these plugins if you don’t have one yet, so you keep your customizations even after updating your theme.

Change all order status after purchase

If your eCommerce store isn’t integrated with any payment gateway that uses order status, you can put all the orders on hold right after the customer place an order, instead of leaving it as “Processing”. Let’s have a look at this script:

function   QuadLayers_change_order_status( $order_id ) {  
                if ( ! $order_id ) {return;}            
                $order = wc_get_order( $order_id );
                if( 'processing'== $order->get_status() ) {
                    $order->update_status( 'wc-on-hold' );
                }
}
add_action('woocommerce_thankyou','QuadLayers_change_order_status');

We use the woocommerce_thankyou hook to trigger our function right after an order has been placed,  and change the status using update_status().

As you can see, the status slug has a prefix (WC). Even though the function also works without the prefix, it’s a recommended practice to use it.

woocommerce-order-status-on-hold

It’s worth noting that you can use any other status and even custom ones instead of “On hold” by adjusting the code.

Modify order status from order ID

The following script will change the status of a single order. For example, to change the order status of order 115, we use the following snippet:

add_action('init',function(){
	$order = new WC_Order(115);
        $order->update_status('wc-processing'); 
});

This is a short script, so we’ve used an anonymous function in the 'init'WordPress hook.

Note that this won’t allow you to make further changes to the status while the script is enabled.

Update WooCommerce order status for returning customers

This is another interesting example to change order status automatically in WooCommerce. The below script will change the order status to “Completed” only if the user has a previous order attached with the “Completed” or “Processing” status.

function QuadLayers_order_status_returning( $order_id ) {
        // Get this customer orders
         $user_id = wp_get_current_user();
        $customer_orders = [];
        foreach ( wc_get_is_paid_statuses() as $paid_status ) {
            $customer_orders += wc_get_orders( [
                'type'        => 'shop_order',
                'limit'       => - 1,
                'customer_id' => $user_id->ID,
                'status'      => $paid_status,
            ] );
        }
            # previous order exists
            if(count($customer_orders)>0){ 
                     if ( ! $order_id ) {return;}            
                     $order = wc_get_order( $order_id );
                     if( 'processing'== $order->get_status() ) {
                         $order->update_status( 'wc-completed' );
                     }
            }
}
add_action( 'woocommerce_thankyou', 'QuadLayers_order_status_returning' );

This can be a good idea to add a layer of security and improve returning customers’ shopping experience.

Change the order status on a URL parameter

This sample script will change to a specific order status when the URL parameter is present on the browser. As we are using the init WordPress hook, the script will work on any page of the store.

Additionally, it will edit the order status for the latest order of the currently logged-in user. In this example, the order status will change to “Cancelled” when any URL has the “revert” parameter like this: https://website.com/shop?st=revert

add_action('init',function(){
    if(isset($_GET['st'])&&!empty($_GET['st']) ):
        $get_url = $_GET['st'];
 
        if($get_url=='revert'):
            $user_id = wp_get_current_user();
            $order = wc_get_customer_last_order($user_id->ID);  
            $order->update_status( 'wc-cancelled' );
        endif;
    endif;
});

Create a custom order status

Instead of changing the status of an order, in this example, we’ll create a new custom order status that we can use freely. This is a good idea if the existing statuses aren’t enough or you prefer to create a new one for more clarity. Once you create a custom order status, you can use it with any of the above scripts.

The following script will register and add a new status to the order status list. In this case, we will call the new custom order status “In progress”, but you can use any name you want by simply customizing the code.

// Register new status
function register_in_progress_order_status() {
    register_post_status( 'wc-in-progress', array(
        'label'                     => 'In progress',
        'public'                    => true,
        'show_in_admin_status_list' => true,
        'show_in_admin_all_list'    => true,
        'exclude_from_search'       => false,
        'label_count'               => _n_noop( 'In progress (%s)', 'In progress (%s)' )
    ) );
}
// Add custom status to order status list
function add_in_progress_to_order_statuses( $order_statuses ) {
    $new_order_statuses = array();
    foreach ( $order_statuses as $key => $status ) {
        $new_order_statuses[ $key ] = $status;
        if ( 'wc-processing' === $key ) {
            $new_order_statuses['wc-in-progress'] = 'In progress';
        }
    }
    return $new_order_statuses;
}
add_action( 'init', 'register_in_progress_order_status' );
add_filter( 'wc_order_statuses', 'add_in_progress_to_order_statuses' );



woocommerce-custom-order-status

Other ways to change the WooCommerce order status

If you’re looking for alternative ways to update the default order status in WooCommerce, you can check out some plugins. Unfortunately, there aren’t many free tools for this but these are some of the best ones:

  • YITH Custom Order Status: An excellent tool to manage order statuses and create custom ones. It’s one of the best tools in the market but it’s not cheap. It will set you back 79.99€ per year.
  • WooCommerce Order Status Manager: Another great plugin to add, remove and edit order status. You can also trigger emails based o the status of the orders. It costs 49 USD per year.
  • Custom Order Status for WooCommerce: One of the best tools to manage order status in WooCommerce. It’s a premium plugin that starts at 39 USD per year.
  • Ni WooCommerce Custom Order Status: One of the few reliable free plugins that let you manage your order status with ease.

How to see the order status in your WooCommerce store

The easiest way to see the order status in your store is to open your WooCommerce dashboard and go to Orders. Then, open any order, and under the General section, press the Status dropdown. There you will see all the orders in your store.

Conclusion

In summary, the order status is a tag that represents the current state of an order. Even though WooCommerce includes certain default statuses, customizing them can be a good idea for those who want to remove or add new ones. It can also be an interesting solution for businesses that need an automated solution for their orders status management.

In this guide, we’ve seen all the default order statuses and different examples to change order status automatically in WooCommerce. These are just some ideas but there’s a lot more you can do. We recommend you take these scripts as a base and play around to customize them for your store.

For more information on how to customize your store, have a look at these tutorials:

Have you updated the order status in your store? Did you have any issues following this guide? Let us know in the comments below!

Hello!

Click one of our representatives below to chat on Telegram or send us an email to [email protected]

How can I help you?