WooCommerce: Custom Order Status

WooCommerce contains 8 different order statuses.

  1. Pending Payment
  2. Processing
  3. On Hold
  4. Completed
  5. Cancelled
  6. Refunded
  7. Failed
  8. 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' ),
			)
		);
	}
PHP

But 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 );
}
PHP

So, 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;
	}
PHP

Thats it!

Thanks.

Istiaq Nirab

A highly skilled and proficient WordPress developer with over Three years of professional experience in all aspects of WordPress website creation, including design, plug-ins, and implementation. Results-driven individual contributor with a successful track record in exceeding expectations for web development initiatives

More Reading

Post navigation

Leave a Comment

Leave a Reply

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