Autocomplete WooCommerce Orders [The Easy Way]

WooCommerce autocomplete orders

If you’re using WooCommerce to sell products you may notice that the orders aren’t automatically marked as complete whenever the customer makes a successful purchase. While it may make sense for physical products that need time to arrive, it doesn’t make much sense for virtual products like eBooks, PDFs, etc.

In this article, we’re going to look at how we can automate this process so that whenever there is a successful purchase the order will be marked as completed.

Mark Orders As Complete Right After The Payment

In order to do that, we’re going to use a little code snippet. You need to add it to your theme’s functions.php file. Alternatively, you can use a plugin like Code Snippets to add the code.

Method 1: Autocomplete Any WooCoomerce Order After Customer Completes Their Payment

<?php

add_action('woocommerce_thankyou', 'my_autocomplete_order');

function my_autocomplete_order($order_id) {
    if ( ! $order_id ) {
        return;
    }

    $order = wc_get_order($order_id);
    $order->update_status('completed');
}

The code snippet above will mark any order as completed as soon as the customer completes the payment on the Thank You page.

Method 2: Autocomplete Only Virtual Orders

This snippet below will show how to complete the order if there are only virtual products in it.

For example, if a customer has bought 3 eBooks and one physical book that needs to be delivered, this snippet won’t mark the order as completed. But if the order doesn’t have any physical product like a book, then the order will be marked as completed as soon as the customer pays.

<?php

add_action('woocommerce_thankyou', 'my_autocomplete_order');

function my_autocomplete_order($order_id) {
    if ( ! $order_id ) {
        return;
    }

    $order = wc_get_order( $order_id );
 
    // get each product in the order
    $items = $order->get_items();
    
    // variable to check if order is virtual
    $is_virtual_order = true;
 
    foreach ($items as $item) {
 
        // get product id
        $product = wc_get_product($item['product_id']);
 
        // is a virtual product
        $is_virtual = $product->is_virtual();

        // if at least one product in the order is not virtual, then the order isn't virtual
        if (! $is_virtual) {
            $is_virtual_order = false;
        }
 
    }
 
    if ($is_virtual_order) {
        $order->update_status('completed');
    }

}

IMPORTANT NOTICE: Make sure you back up your site before making these changes. In case anything goes wrong, you’ll always be able to revert the changes.

Conclusion

In this tutorial, you’ve seen 2 ways how to mark WooCommerce orders as completed: one for any order and another for only virtual orders. Try this method on your site and see how it works for you.

About The Author

Andriy Haydash

Andriy Haydash

Andriy Haydash is a WordPress Expert who helps people build and launch successful WordPress membership and e-learning websites.

Note: Not all of the articles are written directly by me.
Affiliate Disclaimer: Some links in the post may be my affiliate links

The Ultimate Managed Hosting Platform

Before YOU Leave...
Join My Newsletter

Get practical tips & tricks on how to start, grow and market your course/membership site.