Automatically set WooCommerce orders to Completed status

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

 

Avec ce nouveau snippet, nous allons vous montrer comment faire en sorte que vos commandes répondant à certains critères passent directement du status « En cours » au statut « Terminé ». C’est très pratique si vous vendez uniquement des produits virtuels.

WooCommerce passe par défaut les commandes au statut « Terminé » si elle ne contiennent que des produits virtuels et téléchargeables. Cela ne fonctionne cependant pas si les produits ne sont pas marqués comme téléchargeables. Le snippet présenté ci-dessous permettra de passer les commandes au statut « Terminé » si elles ne contiennent que des produits virtuels.

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.

Assurez-vous de cocher la case « Run snippet everywhere » avant d’activer le 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

Leave a Comment

You must be logged in to post a comment.