How to automatically change the status of an order to COMPLETED

Published On: 2 de August de 2021Categories: EcommerceTags: ,

When WooCommerce does not assign the status COMPLETED to orders made through a payment gateway that we have configured (for example paypal, stripe, etc ...) we can create a function so that these orders are automatically placed in COMPLETED.

Although there are several ways to create functions in WordPress, in this article we are going to show you the simplest and safest way. And this way is using a plugin called Code Snippets.

The first step is to install the plugin from the administration of our WordPress -> Plugins -> Add New -> We search for «Code Snippets» and we install this:

After installing we go to the administration panel -> Code Snippets and we press the button Add new. There we put a name to our function and paste the following code:

// Automatically update the order status to COMPLETED
add_action ('woocommerce_order_status_processing', 'update_order_status_to_completed');
add_action ('woocommerce_order_status_on-hold', 'update_order_state_to_completed');
function update_order_state_to_completed ($order_id) {
global $woocommerce;

// ID's of the payment gateways that it affects, I'll explain it to you below
$paymentMethods = array ('paypal', 'stripe');

if (! $order_id) return;
$order = new WC_Order ($order_id);

if (! in_array ($order-> payment_method, $paymentMethods)) return;
$order-> update_status ('completed');
}

Being that way:

When saving this code we would already have the function enabled. Note: The code is ready to apply for Paypal and Stripe. If we need to cover some other gateway, we must specify it in the line where the gateways are specified. Which is this:

// ID's of the payment gateways that it affects, I'll explain it to you below
$paymentMethods = array ('paypal', 'stripe');

We hope this article is of use to you. Please comment if it helped and remember that you can request our support at any time.

About the Author: Che Ramphis

Founder and CEO of Chezaad, LLC. My passion is to help companies and businesses in any area to improve their digital processes to achieve their goals. I define myself as an inveterate learner.

Join the newsletter.

We share valuable information.