Order statuses are maybe one of the most important aspects of WooCommerce and other e-commerce platforms. They give the shop manager a real-time feedback about what needs to be done in all cases. Important and obvious statuses already come as default in WC – such as pending, completed, cancelled and more. And for any status we can’t find, WC’s API allows us to add and configure new ones.

Having that said: it could be easier, not in terms of complexity (it’s easy), but in terms of speed. But let’s recap first how to create a new custom order status, using the existent WordPress and Woocommerce functions and hooks.

The usual way

Once in the past, WC order statuses were a taxonomy of the custom post type shop_order. From WC 2.2 onwards, any order status is registered as a custom post status – which means it needs to be registered within the WordPress beforehand, and just then included in Woocommerce as an additional status.

That can be achieved by using the register_post_status() function, and hooking it to the init action. Somehting like this:

// Register new status
function register_custom_order_status() {
    register_post_status( 'wc-custom-status', array(
        'label'                     => 'Custom Status',
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'Custom Status (%s)', 'Custom Status (%s)' )
    ) );
}
add_action( 'init', 'register_custom_order_status' );

Great… but this is just the first step. Additionally, you now need to include the status inside the array in which all WC statuses are registered. So, let’s do that in a traditional way:

// Add to list of WC Order statuses
function add_custom_status_to_order_statuses( $order_statuses ) {
 
    $new_order_statuses = array();

    foreach ( $order_statuses as $key => $status ) { 
        $new_order_statuses[ $key ] = $status;
        $new_order_statuses['wc-custom-status'] = 'Custom Status';
    } 
    return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'add_custom_status_to_order_statuses' );

Also, you maybe want those new statuses to appear in different colors in the order list page, or have a particular icon and figure as order action, in the same list table. Ultimately, sometimes we need to hook functions and processes on those new statuses… that means we’ll be repeating code for quite a long time.

So, let’s try a different approach: a library.

A smarter way

Of course we also consider Woocommerce a top notch by all means, however they are not necessarily concerned about making every single detail of the code a one-liner. If so, you’d probably never bought any of their extensions, additional plugins or even hire any specialist (if you are not).

We don’t have the same problem. And we hate coding the same dozens of times, if we can do just once. So, try to imagine all of the coding above being replaced by something like this:

use WPH\WC\OrderStatus;
// Am excluding the Composer autoload - which actually should be here

$cos = new OrderStatus('Custom Status');

Promise you – this one-liner will do EXACTLY the same you could reach from both boxes of code we described in the usual way. I’m serious. You will be able to try it yourself by the end of this article.

Want more? Right. Let’s try to add a single array with one or two values in the previous one-liner. The first element relates to the background color of the order status badge, in the order list page. The second element, if added, will change the font color of the same badge.

use WPH\WC\OrderStatus;
// Am excluding the Composer autoload - which actually should be here

$cos = new OrderStatus('Custom Status', [ 'orange', '#ededed' ]);

Custom order status registered, available for WC orders and colored for better use within the order table. And again: ONE LINE OF CODE. The result will be something like the image.

Remember: the first value of the array changes the background color, while the second changes the font color. You can use predefined CSS color names or hex values.

Ok, how can use this library?

We mentioned before: Composer or directly accessing and downloading it from Github (see the widget in the end of the article). Of course, expect new developments, as we are working in some other features related to custom order statuses – which will be added to this very library.

Leave a comment

Your email address will not be published. Required fields are marked *