Automatically set WooCommerce orders to Completed status

Published on: 3 December 2024. Last updated date: 3 December 2024

 

With this new snippet, we’ll show you how to make your orders that meet certain criteria go directly from “Processing” to “Completed” status. This is very useful if you only sell virtual products..

WooCommerce defaults to “Completed” status for orders that only contain virtual and downloadable products. However, this doesn’t work if the products aren’t marked as downloadable. The snippet below will allow orders to be set to “Completed” status if they only contain virtual products.

1|Information

Before using this snippet, make sure you have installed the Code Snippets plugin (free) on your WordPress site. If not, you can download it via the link below. If you have never added a specific feature to your WordPress site, we recommend you start by reading our dedicated guide (link below).


2|Prérequis

Here is the list of extensions required for this snippet to work properly. Make sure to install them before activating the snippet.

WooCommerceNecessary for selling virtual products on your site

3|Snippet

Here is the snippet to use. The comments inside the code will guide you on how this snippet works.

Make sure to check the “Run snippet everywhere” box before enabling the snippet.

/** Here, we plug into the WooCommerce hook that is called when the status of an order is changed. We only do something if it was changed to processing */
\add_action('woocommerce_order_status_changed', function($orderId, $from, $to, $order)
{
    // Enusure Order data
    if ($orderId && !$order) {
        $order = \wc_get_order($orderId);
    }
    // Make sure we receive an order Id
    if (!$order) {
        return;
    }
    // only fo processing
    if ('processing' !== $to) {
        return;
    }
    foreach( $order->get_items() as $item ) {
        if ( $item['type'] == 'line_item' ) {
            $product = $order->get_product_from_item( $item );
            if ( ! $product->is_virtual() ) {
                // If there's a non virtual product
                return;
            }
        }
    }
    $order->update_status( 'completed' );  
}, PHP_INT_MAX, 4);

Our Plugins

We have created powerful and widely acclaimed plugins for WooCommerce. Boost your sales with our solutions

WooRewards

Discover the most powerful loyalty plugin for WooCommerce. Simple or tiered systems, referrals, social networks, badges and achievements, you will find all the tools to build YOUR loyalty program

Learn More

VIP Memberships

VIP Memberships is a complete membership management tool for your WooCommerce site. Sell subscriptions to your customers and offer them benefits such as preferential prices or exclusive products.

Learn More

Virtual Wallet

Offer your customers a virtual wallet on your website. Let them save money by purchasing your products and use this credit on future purchases. This extension also offers a complete gift card tool

Learn More

Referral Codes

Win new customers with this complete SEO tool. Whether through influencers or simple referrers, reward them and the new customers they bring

Learn More
Posted in ,

Long Watch Studio

Leave a Comment

You must be logged in to post a comment.