Auto-Complete WooCommerce Orders

By default, WooCommerce only auto-completes orders that contain only virtual downloadable products. All other orders must be manually marked as complete. However, if your store has a lot of orders that don’t require any processing, having to manually complete lots of orders every day can become a very time consuming task!

Luckily, WooCommerce provides a hook for you to use to auto-complete orders by adding a code snippet to your site. If you’re not comfortable adding custom code to your site, you can also easily auto-complete orders using the WooCommerce Order Status Control plugin. But back to the code snippet, let’s look at a few usage examples. All of the examples use the woocommerce_payment_complete_order_status hook, so that you only auto-complete orders that have actually been paid for (like PayPal, Stripe etc) and not ones that require you to manually verify that payment has been received (like BACS and Cheque).

Auto-completing all orders

First of all, let’s look at how you can get all orders, regardless of content, to auto-complete once they have been paid for. Just use the following code snippet:

add_action( 'woocommerce_payment_complete_order_status', 'snippetpress_auto_complete_paid_order' );
function snippetpress_auto_complete_paid_order() {
    return 'completed';
}

Now every order will auto-complete once paid for.

Auto-completing only virtual orders

To auto-complete orders that only contain virtual products, use the following code snippet:

add_action( 'woocommerce_payment_complete_order_status', 'snippetpress_auto_complete_virtual_order', 10, 2 );
function snippetpress_auto_complete_virtual_order( $status, $order_id ) {   
    $order = wc_get_order( $order_id );
    $order_items = $order->get_items();  
    foreach ( $order_items as $item ) {
        $product = $item->get_product();
        if ( ! $product->is_virtual() ) {
            break;
        }
        $status = 'completed';
    }   
    return $status;
}

Now if the order contains only virtual products, it will be auto-completed. If the order contains a mix of virtual and none virtual products, it won’t auto-complete.

Auto-complete orders for specific products

To auto-complete orders that only contain specific products, use the following snippet, replacing the product IDs:

add_action( 'woocommerce_payment_complete_order_status', 'snippetpress_auto_complete_specific_products_order', 10, 2 );
function snippetpress_auto_complete_specific_products_order( $status, $order_id ) { 
    $order = wc_get_order( $order_id );
    $product_ids = array( // Replace the numbers below with the IDs of the products you want to auto-complete. You can have as many or as few products as you like!
        10,
        18,
        28,
        46,
    );
    $order_items = $order->get_items();
    foreach ( $order_items as $item ) {
        $product_id = $item->get_product_id();
        if( ! in_array( $product_id, $product_ids ) ){
            break;
        }
        $status = 'completed';
    }   
    return $status;
}

Where to add the snippet?

Whichever snippet you choose to use, you should place it at the bottom of the functions.php file of your child theme. Make sure you know what you’re doing when editing this file. Alternatively, you can use a plugin such as Code Snippets to add the custom code to your WordPress site. If you need further guidance on how to add the code, check out our post on How to Add WordPress Snippets.