How to change WooCommerce order status programmatically
This article will help you to change the order status for WooCommerce orders programmatically. It will also teach you to get order status details for WooCommerce orders.
WooCommerce order status is an important meta field that stores the status of a customer’s order. The status can be viewed at the front and back end of your website via the “My Account” area in the account dashboard of your WooCommerce store. It is generally used to indicate the current state of the order. In this tutorial, we will show you how to change WooCommerce order status programmatically using some PHP code.
It is very easy to change or edit the order status in the WooCommerce dashboard. All you have to do is go to the specific order in your WooCommerce -> Orders section, open the specific order, and click the status drop-down to select the new order status.
However this works only on a single order basis, what about if you have thousands of orders that you need to change the order status of. If you want to change the status in bulk, there’s no way of doing so without some pretty complicated code or dedicated WordPress plugin. Or if you are trying to automate the change in order status from “on-hold” to “pending payment” or some other custom order status once a product vendor has marked an order as fulfilled for example. It is possible to update to the “pending” status after an action has been made with some custom code.
You can change the order status of a WooCommerce order programmatically using the WC_Order class. Call the function with the correct parameters, and it should work for you:
$order = new WC_Order($order_id); if (!empty($order)) { $order->update_status( ‘completed’ ); }
Possible order status values: processing, on-hold, cancelled, completed. If you have created your own custom status via code or dedicated WooCommerce plugin you should be able to change the order status to one of those particular status.
You can find more details about it here: WooCommerce Code Reference.
Make sure that you add the code snippet to your WordPress child theme functions.php file or if you have a dedicated snippet plugin you can add it in there instead.
Filter customers by order status
To filter your WooCommerce customer list by order status, start by selecting the “Order Status” filter from the Users Insights filter options. This will present you with a list of all available order status options, where you can choose the filter operator you want to apply.
You can opt for either the “include” or “exclude” operator. The “include” operator will display all customers with an order matching the selected status. For instance, using the “Order Status includes Pending” filter will show a list of customers who currently have an order with a pending status.
Orders by status report
In Users Insights the “Orders by Status” report section visualizes the distribution of order statuses over time using color-coded segments. This report is available in the general WooCommerce reports section for all products, as well as in the per-product and per-category report sections.
You can view it in daily, weekly, monthly, or yearly intervals, with the ability to scroll through historical data.
Conclusion
WooCommerce allows you to create your own customized store and sell products online and it has tons of functionality that can be extended by utilising the API. In this tutorial we looked at updating the programmatically to a in an automated and dynamic way.