Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Customize WooCommerce Order Status Based on Product Tags?

Change WooCommerc Order Status

Certain products distinguished with special product tags need special attention in the order fulfillment process. For example, a grocery store sells products with product tags ‘dairy-products’ and ‘groceries’. In this case, you may need the products with the tags ‘dairy-products’ to be shipped quickly. So as soon as the order is placed with the products containing the tags ‘dairy-products’ the status of these orders gets changed to order status such as ‘ Express Processing’.

Solution: Customize WooCommerce Order Status Based on Product Tags

The code snippet will change the default order status ‘completed’ to the status of ‘Express Processing’ if the order has an item with the product tag ‘dairy-products’.

Note: The implementation can be done either with the default statuses as shown below or you can also create custom WooCommerce order status and implement the same logic within it.

add_action( 'woocommerce_order_status_changed', 'ts_custom_order_status_by_tag', 10, 1 );

function ts_custom_order_status_by_tag( $order_id ) {
    // Get the order object
    $order = wc_get_order( $order_id );

    // Define the target tag ID
    $target_tag_id = 52; // Change this to the ID of your desired tag

    // Get all items in the order
    $items = $order->get_items();

    // Initialize flag to check if product with specific tag is found
    $found_specific_tag = false;

    // Loop through order items
    foreach ( $items as $item ) {
        // Get product id
        $product_id = $item->get_product_id();

        // Get product object
        $product = wc_get_product( $product_id );

        // Get product tags
        $product_tags = $product->get_tag_ids();

        // Check if the product has the target tag
        if ( in_array( $target_tag_id, $product_tags ) ) {
            $found_specific_tag = true;
            break; // Exit the loop if product with specific tag is found
        }
    }

    // If product with specific tag is found, update order status to 'completed'
    if ( $found_specific_tag ) {
        $order->update_status( 'completed' ); // Change 'completed' to your desired status
    }
}
add_filter( 'wc_order_statuses', 'ts_rename_order_status_msg', 20, 1 );
function ts_rename_order_status_msg( $order_statuses ) {
$order_statuses['wc-completed'] = _x( 'Express Processing', 'Order status', 'woocommerce' );

return $order_statuses;
}

Output

When orders are placed with products belonging to the particular product tag as mentioned in the code($target_tag_id = 52;) then the woocommerce order status automatically gets updated to the status ‘Express processing’ as specified in the code.

Change WooCommerce Order status 
based on Product tags

In this post, we are changing the order status flow i.e. from any status set by default to a particular order status. On the other hand, you can also automatically complete all WooCommerce orders when they go to the processing status.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x