Search code examples
phpwordpresswoocommercehook-woocommercebulk

WooCommerce custom order status and add this status bulk action drop down


I have added custom order status in WooCommerce with the code below, and it's working. How can add this order status in WooCommerce bulk action drop-down on the order list page. I want to change the order status bulk.

// Register new order status
function register_on_production_order_status() {
    register_post_status( 'wc-on-production', array(
        'label'                     => 'On Production',
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'On Production (%s)', 'On Production (%s)' )
    ) );
}
add_action( 'init', 'register_on_production_order_status' );

// Add to list of WC Order statuses in single order page
function add_on_production_to_order_statuses( $order_statuses ) {
 
    $new_order_statuses = array();
 
    // add new order status after processing
    foreach ( $order_statuses as $key => $status ) {
 
        $new_order_statuses[ $key ] = $status;
 
        if ( 'wc-processing' === $key ) {
            $new_order_statuses['wc-on-production'] = 'On Production';
        }
    }
 
    return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'add_on_production_to_order_statuses' );

Solution

  • // Add a bulk action to Orders bulk actions dropdown
    add_filter('bulk_actions-edit-shop_order', 'status_orders_bulk_actions');
    
    function status_orders_bulk_actions($bulk_actions) {
        $bulk_actions['on-production'] = __('Change to on production');
        return $bulk_actions;
    }
    
    // Process the bulk action from selected orders
    add_filter('handle_bulk_actions-edit-shop_order', 'status_bulk_action_edit_shop_order', 10, 3);
    
    function status_bulk_action_edit_shop_order($redirect_to, $action, $post_ids) {
        if ($action === 'on-production') {
    
            foreach ($post_ids as $post_id) {
                $order = wc_get_order($post_id);
                $order->update_status('on-production');
            }
        }
        return $redirect_to;
    }