WooCommerce Dynamic Order Status

Welcome back to the WebFix Lab blog, where we continually strive to provide insights and solutions that make your web development journey easier and more effective. In this post, we will focus on a powerful customization for WooCommerce – modifying order status based on certain conditions. This feature can significantly enhance the order management process, making it more streamlined and tailored to your unique business needs.

WooCommerce, while a robust e-commerce solution, may not cater to every unique business requirement out of the box. One such requirement could be the need for dynamic order statuses. While WooCommerce does provide several default statuses like ‘pending’, ‘processing’, ‘on-hold’, ‘completed’, and ‘cancelled’, they might not be sufficient for every business scenario. This is where custom order statuses come into the picture.

Understanding the Business Scenario

Let’s consider a hypothetical online store that has three unique requirements, each corresponding to a specific order status:

  1. Extras Only: The status should be updated to ‘Extras Only’ when all ordered items belong to a hypothetical ‘Extras’ category. This could be a special category in your store with additional items or accessories.
  2. Proc/ Gift Box: If the customer chooses to add a gift box to their order, the status should be updated to ‘Proc/ Gift Box’. The selection of a gift box is done via a custom field (‘_Gift Ready Milk Collection Kit’) added to the product. This could be a checkbox or a select field that the customer can choose while placing the order.
  3. Intl Order: If the order comes from a location outside the US or Canada, the status should be updated to ‘Intl Order’, indicating that it’s an international order.

These are just sample scenarios and you can tailor them according to your specific business requirements. You could have different categories, different custom fields, or different countries based on which you want to change the order status.

To achieve this in WooCommerce, we can write a custom PHP function that checks these conditions and modifies the order status accordingly. We hook this function to the ‘woocommerce_thankyou’ action, which gets triggered after an order is placed.

The PHP Code Explained

Let’s break down the PHP code snippet step-by-step. This code should be placed in your theme’s functions.php file or you can create a custom plugin for this functionality.

add_action( 'woocommerce_thankyou', 'change_status_giftbox_and_extrasonly', 10, 1 );

This line hooks our function change_status_giftbox_and_extrasonly to the woocommerce_thankyou action. The 10 is the priority (i.e., when it should execute in relation to other functions hooked to the same action), and 1 is the number of accepted arguments, in our case, the order ID.

Inside the change_status_giftbox_and_extrasonly function, we have several sections:

if ( ! $order_id ) return;

This line checks if there’s an $order_id. If it doesn’t exist, it immediately ends the function execution.

$order = wc_get_order( $order_id );
if ($order->get_status() !== 'processing') return;

Here, the order object is fetched using the WooCommerce function wc_get_order. If the order status is not ‘processing’, the function ends.

$categories = array( 'extras' );

This line defines specific product categories that are significant for our order status change. In this case, the ‘extras’ category is defined.

if ($order->get_shipping_country() == "US" OR $order->get_shipping_country() == "Canada" OR $order->get_shipping_country() == "CA" OR $order->get_billing_country() == "US" OR $order->get_billing_country() == "Canada" OR $order->get_billing_country() == "CA") {
    $country = "domestic";
}else {
    $country = "international";

This part of the code checks the shipping and billing country of the order. If it is either the US or Canada, the $country variable is set as ‘domestic’, otherwise, it is set as ‘international’.

// Loop through order items
foreach ( $order->get_items() as $item ) {

    // Here, we can get the value by a specific metakey
    if ( $item->get_meta( "_Gift Ready Milk Collection Kit" ) ) {
        // Test if string contains the word 
        if(strpos($item->get_meta( "_Gift Ready Milk Collection Kit" ), "No") === false){
            $found = false;
            $gift_found = true;

    // Product ID
    $product_id = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();

    // Has term (product category)
    if ( !has_term( $categories, 'product_cat', $product_id ) ) {
        $found = false;

The function then loops through each item in the order. Within this loop, it checks for our predefined conditions and sets flags ($found and $gift_found) accordingly.

if ( $found ) {
    $order->update_status( 'extras' );
}elseif ( $gift_found ) {
    $order->update_status( 'process-gift-box' );
}elseif ( !$found && !$gift_found && $country == "international" ) {
    $order->update_status( 'intl-order' );

Finally, based on the flags and country value, the order status is updated. If the ‘extras’ category is found, the status is updated to ‘extras’. If the gift box is selected, the status becomes ‘process-gift-box’. And if neither condition is met and the country is ‘international’, the status is updated to ‘intl-order’.

This code snippet provides a powerful way to automate and customize your WooCommerce order management. By tailoring this function to your specific needs, you can make your order handling process much more efficient.

For the complete PHP code, click here to view the Gist.

Disclaimer: Modifying your theme’s functions.php file may affect your website’s functionality. Always backup your site before making changes, or consider using a child theme to prevent potential disruptions. If you’re not comfortable editing theme files, please consult with a professional.

We hope you found this guide useful for understanding how to implement dynamic order statuses in WooCommerce. Stay tuned to WebFix Lab for more insightful content that empowers you to make the most of your web development projects.