How To Complete WooCommerce Order Automatically After Successful Payment

How to Auto Complete WooCommerce Orders After payment

If are selling virtual products like Courses, Ebooks on your Woocommerce store and you want to auto complete the order when the customer make the payment, then follow this guide.

Let say, when someone order an Ebook on your Ecommerce store by making the payment. But, the Ebook will not be available to download unless the order is marked as completed by the Store owner. So, the user have to wait before he/she can download the file.

You can auto complete your Woocommerce order in Wordpress and give the access to the Download of your Virtual product after the customer make the payment. But, there is no such options available in Woocommerce right now.

YouTube video

For this you need to use a dedicated plugin or use a custom code snippets.

I don’t recommend using a paid plugin for a simple task like this, You can do this with a small php snippet given below.

Login to your WP Dashboard and Here, We need a Code manager plugin to add the custom PHP snippet. You can also Add the code directly in the theme function.php file, But make sure to use a Child theme for that.

First install and Activate this free WP code manager plugin.

WP-Code-Insert-Headers-and-Footers

Now Go to Add Snippet Option and Select the use snippet option as shown below.

How to add Code snippet in WP code plugin in WordPress

Now You need to copy the below code and paste it, make sure to change the code type of PHP snippet and save it.

add_action('woocommerce_thankyou','custom_woocommerce_auto_complete_order');
function   custom_woocommerce_auto_complete_order( $order_id ) {  
                if ( ! $order_id ) {return;}            
                $order = wc_get_order( $order_id );    
                $order->update_status( 'completed' ); 
}

After that All of your WooCommerce orders will be auto completed when the customer make the payment on your Website.

This code is useful for those website who are only selling digital products as we don’t want physical orders to complete automatically without delivering the product to the customer.

If you are selling both Digital and physical product on your website and want autocomplete only on virtual and downloadable products than you can also use the below code.

add_filter( 'woocommerce_payment_complete_order_status', 'auto_complete_virtual_orders', 10, 3 );

function auto_complete_virtual_orders( $payment_complete_status, $order_id, $order ) {
    $current_status = $order->get_status();
    // We only want to update the status to 'completed' if it's coming from one of the following statuses:
    $allowed_current_statuses = array( 'on-hold', 'pending', 'failed' );

    if ( 'processing' === $payment_complete_status && in_array( $current_status, $allowed_current_statuses ) ) {

        $order_items = $order->get_items();

        // Create an array of products in the order
        $order_products = array_filter( array_map( function( $item ) {
            // Get associated product for each line item
            return $item->get_product();
        }, $order_items ), function( $product ) {
            // Remove non-products
            return !! $product;
        } );

        if ( count( $order_products ) > 0 ) { // Corrected line
            // Check if each product is 'virtual'
            $is_virtual_order = array_reduce( $order_products, function( $virtual_order_so_far, $product ) {
                return $virtual_order_so_far && $product->is_virtual();
            }, true );

            if ( $is_virtual_order ) {
                $payment_complete_status = 'completed';
            }
        }
    }
    return $payment_complete_status;
}

How to Auto Complete WooCommerce Order using Plugin?

If You don’t want to use a code snippet and want to use a plugin than you can consider using a plugin like “Autocomplete WooCommerce Orders”.

Autocomplete WooCommerce Orders Plugin

Install and activate this plugin and you will see a new option in your WooCommerce settings like this (as shown below).

How to Auto Complete WooCommerce Order using Plugin

Here, You can select All Orders, Virtual Orders and Both Virtual and downloadable orders in the free version of this plugin.

Setting up Auto complete WooCommerce orders helps Improve customers’ satisfaction as they don’t have to wait for the order confirmation and can download the file right after the payment.

If you have any doubt, feel free to ask in the comment section.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *