How to Add Custom Order Status Programmatically in WooCommerce

Today we are going to discuss how you can add custom order status to your WooCommerce orders programmatically. After reading this article you would be able to customize order data in such a way that you would be able to add additional order status to order data.

Adding a Custom Order Status in WooCommerce

By Default there are these 7 order statuses in WooCommerce that you can assign to any order:
Pending Payment
On hold
Processing
Failed
Completed
Cancelled
Refunded

WooCommerce does not yet allow customers to add new order statuses or modify existing order status. Lets add a new order status “Order Expire”. In order to add a new order status, you will have to add register_post_status() function. Edit function file and add the following code there:

function register_expire_order_status() {
    register_post_status( 'wc-expire', array(
        'label'                     => 'Order Expire',
        'public'                    => true, // show for customer
        'show_in_admin_status_list' => true,
        'show_in_admin_all_list'    => true,
        'exclude_from_search'       => false,
        'label_count'               => _n_noop( 'Order Expire <span class="count">(%s)</span>', 'Order Expire <span class="count">(%s)</span>' )
    ) );
}
add_action( 'init', 'register_expire_order_status' );

function add_expire_order_statuses( $oldOrderStatus ) {

    $orderStatus = array();

    foreach ( $oldOrderStatus as $key => $status ) {

        $orderStatus[ $key ] = $status;

        if ( 'wc-completed' === $key ) {
            $orderStatus['wc-expire'] = 'Order Expire';
        }
    }

    return $orderStatus;
}
add_filter( 'wc_order_statuses', 'add_expire_order_statuses' );

This article discussed how to add a new custom order status in WooCommerce. If you have any issues executing this code then feel free to contact me.

Search

Recent Blogs

Categories

Archives

Archives

Tags