5/5

WooCommerce Profit Reports
Like You've Never Seen Before

Mark Orders As Complete As Soon As They're Submitted In WooCommerce

If you are selling software or other downloadable / virtual products, there’s not really any need to have the order “processing”.

As soon as the customer checks out, the product is usually delivered.

Therefore, it makes sense for us to just automatically mark it as complete.

The below snippet will show you exactly how to do this.

Copy and paste the following code into your functions.php file.

/**
 *
 *  @author     Christopher Davies, WP Davies
 *  @link       https://wpdavies.dev/
 *  @link       https://wpdavies.dev/automatically-complete-virtual-orders-woocommerce/
 *  @snippet    Automatically complete orders in WooCommerce
 *
 */
add_action('woocommerce_thankyou', 'wpd_autocomplete_virtual_orders', 10, 1 );
function wpd_autocomplete_virtual_orders( $order_id ) {
 
    if( ! $order_id ) return;
 
    // Get order
    $order = wc_get_order( $order_id );
 
    // get order items = each product in the order
    $items = $order->get_items();
 
    // Set variable
    $only_virtual = true;
 
    foreach ( $items as $item ) {
         
        // Get product object
        if ( isset($item['variation_id']) && ! empty($item['variation_id']) ) {

            $product = wc_get_product( $item['variation_id'] );

        } else {

            $product = wc_get_product( $item['product_id'] );

        }

        // Safety check
        if ( ! is_object($product) ) {

            return false;

        }
                
        // Is virtual
        $is_virtual = $product->is_virtual();
 
        // Is_downloadable
        $is_downloadable = $product->is_downloadable();
 
        if ( ! $is_virtual && ! $is_downloadable  ) {
 
            $only_virtual = false;
 
        }
 
    }
 
    // true
    if ( $only_virtual ) {
 
        $order->update_status( 'completed' );
 
    }

}

Automatically Complete WooCommerce Orders

That’s basically it, the critical thing is toward the end of the snippet where it says $order->update_status( ‘completed’ );

This is the section which actually performs the action, everything before it is basically looping through the order to check that all the products are downloadable or virtual.

You can modify this to set the status to whatever you want, you can find a list of the standard WooCommerce statuses here.

And That's How We AutoComplete WooCommerce Orders

Happy WooCommercing friends!

If you have any questions or it’s not working for you feel free to leave a comment below and I will be able to assist you.

Thanks, Chris!

Related Post

Subscribe
Notify of
guest
15 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Charlie
Charlie
1 year ago

This doesn’t seem to be working in the latest version of Woocommerce (6.8.2).

Taka
Taka
1 year ago

Hi,
Thanks for great codes!

I have 2 questions.

  1. What happens if customer dont pay money choosing bank transfer? Still Code automatically sent to customer?
  2. My product is virtual product but not downloadable. How can I fix this code?
Sean
Sean
1 year ago

Small addition to the code in case you happen to use the woocommerce subscriptions addon for your virtual service/product: /**  *  * @author    Christopher Davies, WP Davies  * @link      https://wpdavies.dev/  * @link      https://wpdavies.dev/automatically-complete-virtual-orders-woocommerce/  * @snippet   Automatically complete virtual, downloadable, and subscription orders in WooCommerce. This stops them from being in "Processing" waiting for a manual completion  *  */ add_action('woocommerce_thankyou', 'wpd_autocomplete_virtual_orders', 10, 1 ); function wpd_autocomplete_virtual_orders( $order_id ) {    if( ! $order_id ) return;    // Get order    $order = wc_get_order( $order_id );    // get order items = each product in the order    $items = $order->get_items();    // Set variable    $only_virtual = true;    foreach ( $items as $item ) {        //… Read more »

Jakob
Jakob
1 year ago

If I sell products that is only set to virtual (happens when I sell event tickets from events plugin integrated with woocommerce), can I also set these orders to completed – or will they already work like that?

Jakob
Jakob
Reply to  christopher
1 year ago

Great. Thank you!

khaled
khaled
1 year ago

how can i make it work for free items only?

Matt
Matt
11 months ago

Thanks Chris, worked perfectly!

TKNR
TKNR
9 months ago

Hi,
Christopher.

Thank you for great code.
Could I ask one question?

When I manually change an order from Pending to Processing, it does not automatically complete.

How can I solve this?

I really need this feature for several reasons.

Lindsay
Lindsay
5 months ago

Please note this does not check variable products. If you want to check if BOTH simple products and variations are virtual, you need to update the code as follows:

Remove this line:

$product = wc_get_product( $item['product_id'] );

And replace it with this:

$product_variation_id = $item['variation_id'];
if ($product_variation_id) {
$product = wc_get_product($item['variation_id']);
} else {
$product = wc_get_product($item['product_id']);
}

Last edited 5 months ago by Lindsay

Recent Posts