WooCommerce contains 8 different order statuses.
- Pending Payment
- Processing
- On Hold
- Completed
- Cancelled
- Refunded
- Failed
- Draft
For most of the context, it’s fine But sometimes you need some extra status for specific reasons.
For example, we need 2 more extra statuses which represent order is In shipment or Shipment Failed
To create custom order status, You need to know that WooCommerce Orders are basically a custom post type(shop_order). So, we need to register the custom status in WordPress way first, then register it in WooCommerce.
Here is the example of registering custom post status for WooCommerce Order via register_post_status function.
add_action( 'init', 'register_order_status' );
/**
* Register Custom Order Status.
*/
function register_order_status() {
register_post_status(
'wc-in-shipment',
array(
'label' => __( 'In Shipment', 'text-domain' ),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
// translators: count of wc-in-shipment status.
'label_count' => _n_noop( 'In Shipment <span class="count">(%s)</span>', 'In Shipment <span class="count">(%s)</span>', 'text-domain' ),
)
);
register_post_status(
'wc-shipment-failed',
array(
'label' => __( 'Shipment Failed', 'text-domain' ),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
// translators: count of wc-shipment-failed status.
'label_count' => _n_noop( 'Shipment Failed <span class="count">(%s)</span>', 'Shipment Failed <span class="count">(%s)</span>', 'text-domain' ),
)
);
}
PHPBut WooCommerce can’t get our statuses. So, need to include statuses via the wc_order_statuses filter hook. This hook definition can be found in woocommerce/includes/wc-order-functions.php:106
/**
* Get all order statuses.
*
* @since 2.2
* @used-by WC_Order::set_status
* @return array
*/
function wc_get_order_statuses() {
$order_statuses = array(
'wc-pending' => _x( 'Pending payment', 'Order status', 'woocommerce' ),
'wc-processing' => _x( 'Processing', 'Order status', 'woocommerce' ),
'wc-on-hold' => _x( 'On hold', 'Order status', 'woocommerce' ),
'wc-completed' => _x( 'Completed', 'Order status', 'woocommerce' ),
'wc-cancelled' => _x( 'Cancelled', 'Order status', 'woocommerce' ),
'wc-refunded' => _x( 'Refunded', 'Order status', 'woocommerce' ),
'wc-failed' => _x( 'Failed', 'Order status', 'woocommerce' ),
);
return apply_filters( 'wc_order_statuses', $order_statuses );
}
PHPSo, Let’s include our statuses
add_filter( 'wc_order_statuses', 'wc_custom_order_status' );
/**
* Add custom order status.
*
* @param Array $order_statuses order statuses.
*
* @return Array
*/
function wc_custom_order_status( $order_statuses ) {
$order_statuses['wc-in-shipment'] = _x( 'In Shipment', 'Order status', 'text-domain' );
$order_statuses['wc-shipment-failed'] = _x( 'Shipment Failed', 'Order status', 'text-domain' );
return $statuses;
}
PHPThats it!
![](https://script-jungle.com/wp-content/uploads/2023/09/woocommerce-custom-order-status.png)
Thanks.
Leave a Comment