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

How to Customize WooCommerce Order Status for Specific Products?

Chnage WooCommerce Order Status for Specific Products

If you are looking to distinguish a particular product from your admin orders page then you need to customize the WooCommerce order status whenever this product is in the order items. For instance, if you store is offering pre-order products that is not released or unstocked then you can change the status for those products as completed.

Solution: Change WooCommerce Order Status for Specific Product

The code snippet below helps to change the order status of a particular product with the ID to ‘completed’ status when it is included in the order items.

add_action('woocommerce_order_status_changed', 'ts_update_order_status', 10, 4);

function ts_update_order_status($order_id, $old_status, $new_status, $order)
{
    // 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 ID is found
        $found_specific_product = false;

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

            // Check if the product ID matches the specific product ID
            if ($product_id == 1058) {
                $found_specific_product = true;
                break; // Exit the loop if product with specific ID is found
            }
        }

        // If product with specific ID is found, update order status to 'completed'
        if ($found_specific_product) {
            $order->update_status('completed');
        }
    }
}

Output

Let’s consider that I have set a product T-shirt as a pre-order item. When the order items contain the specific product T-shirt with the product ID: 1058 (specified in the code), the order status of these orders is automatically changed to ‘completed’. This way you can easily identify the pre-order items from your WooCommerce admin orders page.

How to Customize WooCommerce Order Status for Specific Products? - Tyche Softwares

If you wish to make simple customizations just like the above one then with these handy code snippets you can also customize WooCommerce order status based on product categories 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