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

How to Automatically Complete Order in WooCommerce?

The default WooCommerce sets the order status to “on hold” when an order is completed. However, customers expect their orders to be completed immediately, especially for virtual products or downloadable files. In such cases, it may be necessary to automatically change the order status to “complete” immediately after the order is placed. This ensures that customers can access or use their purchased items without any delay.

In this post, you will learn how to automatically complete order in WooCommerce.

Where to Add Custom Code in WooCommerce?

It is advisable to add the code snippets to the functions.php file of your child theme. Access the file directly from Appearance->Theme File Editor->Locating the child theme’s functions.php from the right sidebar. You can also access it from your theme’s directory file. Insert the following code snippet in functions.php. The alternative & easy option is to install & activate the Code Snippets plugin. You can then add the code as a new snippet via the plugin.

Solution: Automatically Complete Order in WooCommerce

For online stores selling software or other downloadable/virtual products, the order status “processing” is not required. These products are typically delivered to the customer immediately upon completion of the checkout process. Therefore, it is logical to automatically mark the order as complete.

To implement this automatic order completion feature for software or other downloadable/virtual products, you can utilize the code snippet provided below.

// Hook to trigger the order status update
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)) {
        // Perform your custom actions here
        // For example, you can update some order meta data
        update_post_meta($order_id, '_custom_meta_key', 'Custom Meta Value');

        // You can also update the order status to a new status
        $new_order_status = 'completed'; // Change this to your desired new status
        $order->update_status($new_order_status);
    }
}

Output

Front-End Section

During the checkout process, when a customer places an order for a digital product (software), the status of the order gets automatically updated to ‘Completed’.

How to Automatically Complete Order in WooCommerce? - Tyche Softwares

Admin Section

When a customer orders a digital product from the front-end site, the order status is automatically updated to ‘Completed’ in the admin section.

How to Automatically Complete Order in WooCommerce? - Tyche Softwares

Code Explanation

Hook Registration:

The add_action function is used to register a WordPress action hook. In this case, the hook is woocommerce_order_status_changed, which is triggered when the order status changes. The callback function that will be executed when the hook is triggered is ts_update_order_status.

Callback Function:

The ts_update_order_status function is the callback that is executed when the order status changes. It receives four parameters: $order_id (the ID of the order), $old_status (the previous order status), $new_status (the new order status), and $order (the order object).

Target Statuses:

The $target_statuses array contains the order statuses you want to target. In this example, it includes ‘on-hold’ and ‘processing’. You can modify this array to include the statuses you are interested in.

Condition Check:

The if (in_array($new_status, $target_statuses)) statement checks if the new order status is one of the target statuses.

Custom Actions:

Inside the condition, you can perform your custom actions. In this example, it updates a custom meta field (_custom_meta_key) with a value (‘Custom Meta Value’). Additionally, it sets a new order status ($new_order_status) using $order->update_status().

Conclusion

This code automatically sets the order status of digital products (such as software or downloads) to “complete” immediately after a customer checks out. Additionally, you can also automatically complete WooCommerce orders when they go to the Processing status based on the chosen payment gateway.

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

Share It:

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