WooCommerce: Set Custom Order Status for New Orders

 All WooCommerce orders go to either “processing”, “completed”, “on-hold” and other default order statuses based on the payment method and product type.

Sometimes these statuses are not enough. For example, you might need to mark certain orders in a different way for tracking, filtering, exporting purposes. Or you might want to disable default emails by bypassing the default order status changes.

Either way, setting a custom order status automatically once the order is processed is quite easy. And today we’ll see which PHP snippets you need in order to make this work!

WooCommerce: set a custom order status during checkout

PHP Snippet: Assign Custom Order Status during WooCommerce Checkout Process

/**
* @snippet       Set Custom Order Status during Checkout
* @how-to        Get CustomizeWoo.com FREE
* @sourcecode    https://businessbloomer.com/?p=77911
* @author        Rodolfo Melogli
* @compatible    WooCommerce 3.5.4
* @donate $9     https://businessbloomer.com/bloomer-armada/
*/
 
// ---------------------
// 1. Register Order Status
 
add_filter( 'woocommerce_register_shop_order_post_statuses', 'bbloomer_register_custom_order_status' );
 
function bbloomer_register_custom_order_status( $order_statuses ){
    
   // Status must start with "wc-"
   $order_statuses['wc-custom-status'] = array(                                
   'label'                     => _x( 'Custom Status', 'Order status', 'woocommerce' ),
   'public'                    => false,                                
   'exclude_from_search'       => false,                                
   'show_in_admin_all_list'    => true,                                
   'show_in_admin_status_list' => true,                                
   'label_count'               => _n_noop( 'Custom Status <span class="count">(%s)</span>', 'Custom Status <span class="count">(%s)</span>', 'woocommerce' ),                             
   );     
   return $order_statuses;
}
 
// ---------------------
// 2. Show Order Status in the Dropdown @ Single Order and "Bulk Actions" @ Orders
 
add_filter( 'wc_order_statuses', 'bbloomer_show_custom_order_status' );
 
function bbloomer_show_custom_order_status( $order_statuses ) {     
   $order_statuses['wc-custom-status'] = _x( 'Custom Status', 'Order status', 'woocommerce' );      
   return $order_statuses;
}
 
add_filter( 'bulk_actions-edit-shop_order', 'bbloomer_get_custom_order_status_bulk' );
 
function bbloomer_get_custom_order_status_bulk( $bulk_actions ) {
   // Note: "mark_" must be there instead of "wc"
   $bulk_actions['mark_custom-status'] = 'Change status to custom status';
   return $bulk_actions;
}
 
 
 
// ---------------------
// 3. Set Custom Order Status @ WooCommerce Checkout Process
 
add_action( 'woocommerce_thankyou', 'bbloomer_thankyou_change_order_status' );
 
function bbloomer_thankyou_change_order_status( $order_id ){
   if( ! $order_id ) return;
   $order = wc_get_order( $order_id );
 
   // Status without the "wc-" prefix
   $order->update_status( 'custom-status' );
}

No comments:

Post a Comment

Free Shipping Threshold @ Cart Page