Your Dedicated Partner for All Things WordPress

How to Change WooCommerce Order Status Programmatically

Table of Contents

WooCommerce is a popular eCommerce platform that powers millions of online stores worldwide. One of the core features of WooCommerce is the ability to manage orders. By default, WooCommerce comes with several order statuses such as pending, processing, completed, and cancelled. However, there may be times when you need to create custom order statuses or change the order status programmatically. In this blog post, we’ll show you how to change the WooCommerce order status programmatically and give you some examples of its practical applications.

Before we get started, it’s essential to understand how order statuses work in WooCommerce. When a customer places an order, it’s assigned a default status of “pending.” You can manually change this status to “processing” when you start fulfilling the order. Once the order is complete, you can change the status to “completed.” If an order is cancelled, the status is changed to “cancelled.”

Now, let’s say you need to create a custom order status called “on-hold.” You can do this by adding the following code to your functions.php file:

“`php
function register_custom_order_status() {
register_post_status( ‘wc-on-hold’, array(
‘label’ => ‘On hold’,
‘public’ => true,
‘exclude_from_search’ => false,
‘show_in_admin_all_list’ => true,
‘show_in_admin_status_list’ => true,
‘label_count’ => _n_noop( ‘On hold <span class=”count”>(%s)</span>’, ‘On hold <span class=”count”>(%s)</span>’ )
) );
}
add_action( ‘init’, ‘register_custom_order_status’ );
“`

You can then use the following code to change the order status to “on-hold” programmatically:

“`php
$order = wc_get_order( $order_id );
$order->update_status( ‘on-hold’, __( ‘Order on hold’, ‘woocommerce’ ) );
“`

This code retrieves the order object using the $order_id variable and updates its status to “on-hold.”

Now let’s see a practical application of this code. Let’s say you run an online store that offers a subscription service. You want to create a custom order status called “expired” that is assigned to orders when their subscription period ends. You can use the following code to achieve this:

“`php
function change_order_status_on_subscription_expiry( $subscription ) {
$order_id = $subscription->get_parent_id();
$order = wc_get_order( $order_id );
$order->update_status( ‘expired’, __( ‘Subscription expired’, ‘woocommerce’ ) );
}
add_action( ‘woocommerce_subscription_end_of_prepaid_term’, ‘change_order_status_on_subscription_expiry’ );
“`

This code listens for the “woocommerce_subscription_end_of_prepaid_term” event, which is triggered when a subscription period ends. It then retrieves the parent order ID and updates its status to “expired.”

Conclusion

Changing WooCommerce order status programmatically can be a useful feature for online store owners who want to customize their order management system. With the help of WordPress hooks and filters, you can create custom order statuses, automate order status changes, and integrate with third-party services. We hope this article has given you some insights on how to achieve this and inspired you to explore more possibilities with WooCommerce.

 

How to get started?

Learn more

WooCommerce Maintenance

Save 33% with our Annual pricing plan.

Get Started

Having Troubles With WordPress?

Claim Your Free WordPress Maintenance

In today’s fast-paced digital landscape, every website deserves the care and expertise of a professional maintenance team, ensuring optimal performance, enhanced security, and seamless user experiences, so you can focus on growing your business with peace of mind.

Alexey Seryapin
Founder of WPServices

Coupon Code Applied!

Take your time and continue browsing our services.

Alexey Seryapin
Founder of WPServices