WooCommerce is a popular WordPress plugin that allows users to sell products online. While it comes with default order statuses such as cancelled, completed, failed, on-hold, pending, processing, and refunded, users may want to add new statuses or modify existing ones. In this article, we will show you how to create a plugin that enables you to create custom order statuses in WooCommerce.
Previously, WooCommerce used a “shop_order_status” taxonomy to add new order statuses, which was a bit complicated. However, now it’s easier than ever. Here’s an example of how to add a new order status to your WooCommerce orders.
Registering New WooCommerce Order Status
To register new order statuses, you need to add the following code to your functions.php file:
function wpex_wc_register_post_statuses() {
register_post_status( ‘wc-custom-order-status’, array(
‘label’ => _x( ‘Custom Order Status Name’, ‘WooCommerce Order status’, ‘text_domain’ ),
‘public’ => true,
‘exclude_from_search’ => false,
‘show_in_admin_all_list’ => true,
‘show_in_admin_status_list’ => true,
‘label_count’ => _n_noop( ‘Approved (%s)’, ‘Approved (%s)’, ‘text_domain’ )
) );
}
add_filter( ‘init’, ‘wpex_wc_register_post_statuses’ );
The above code registers a new order status called “Custom Order Status Name” with the ID “wc-custom-order-status.” You can change the label and ID to suit your needs.
Adding New Order Statuses to WooCommerce
After registering the new order status, you need to add it to the $order_statuses array in the wpex_wc_add_order_statuses function. Here’s how:
function wpex_wc_add_order_statuses( $order_statuses ) {
$order_statuses[‘wc-custom-order-status’] = _x( ‘Custom Order Status Name’, ‘WooCommerce Order status’, ‘text_domain’ );
return $order_statuses;
}
add_filter( ‘wc_order_statuses’, ‘wpex_wc_add_order_statuses’ );
The above code adds the new order status to the $order_statuses array, which makes it available in the WooCommerce order status dropdown menu.
Adding Multiple New Order Statuses
If you want to add multiple new order statuses, you can duplicate the register_post_status function inside the wpex_wc_register_post_statuses function as many times as you want, making sure to alter the ID and labels accordingly. Then, add the new order statuses to the $order_statuses array in the wpex_wc_add_order_statuses function.
Conclusion
Creating custom order statuses in WooCommerce is easy with the above code snippets. You can add as many new order statuses as you want and modify existing ones to suit your needs. This feature allows you to keep track of your orders more efficiently and provide better customer service.