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 Categories?

This customization is typically required when you want to implement specific actions to the WooCommerce order status for specific category of products present in the order. For example, if the order contains items from the product category of ‘Electronics’ then the code snippet tends to change the order status to ‘Processing’.

Solution: Change WooCommerce Order Status Based on Product Categories

The code snippet below will customize the WooCommerce Order Status to a new status as defined in the code based on the product category of items present in the order.

add_action( 'woocommerce_order_status_changed', 'ts_custom_order_status_by_cat', 10, 3 );

function ts_custom_order_status_by_cat( $order_id, $old_status, $new_status ){
    // Get the order object
    $order = wc_get_order( $order_id );

    // Define the target statuses to check against
    $target_statuses = array( 'on-hold', 'processing' ); // Change this to your desired target statuses

    // Check if the new status is one of the target statuses
    if ( in_array( $new_status, $target_statuses ) ) {
        // Get all items in the order
        $items = $order->get_items();

        // Initialize flag to check if product with specific category is found
        $found_specific_cat = false;

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

            // Check if the product belongs to the 'electronic' category
            if ( has_term( 'electronic', 'your_productcat', $product_id ) ) { // Replace 'electronic' with your desired category slug
                $found_specific_cat = true;
                break; // Exit the loop if product with specific category is found
            }
        }

        // If product with specific category is found, update order status to 'completed'
        if ( $found_specific_cat ) {
            $order->update_status( 'completed' ); // Change 'completed' to your desired status
        }
    }
}

Output

When an order is placed the code checks if any of the items belongs to the specified category of ‘Electronics’ using the has_term() function. If the products with the specified category is found, the order status is updated from the new status of ‘on-hold’ or anything else that is set to the status of ‘Processing’.

Change WooCommerce Order Status

Similar to this functionality, you can also customize WooCommerce order status for specific products or customize WooCommerce order status based on product tags.

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