WooCommerce change status for BACS orders based on specific shipping methods

426 views Asked by At

How do I change the order status from on hold to my own custom status for a specific shipping method if the selected payment gateway is BACS?

This is how I added my own custom status:

// Register New Order Status
 
add_filter( 'woocommerce_register_shop_order_post_statuses', 'register_custom_order_status' );
 
function register_custom_order_status( $order_statuses ){
    
   // Status must start with "wc-"
   $order_statuses['wc-custom-status'] = array(                                 
   'label'                     => _x( 'Calculating Shipping', '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( 'Calculating Shipping <span class="count">(%s)</span>', 'Calculating Shipping <span class="count">(%s)</span>', 'woocommerce' ),                              
   );      
   return $order_statuses;
}
 

// Show Order Status in the Dropdown @ Single Order and "Bulk Actions" @ Orders
 
add_filter( 'wc_order_statuses', 'show_custom_order_status' );
 
function show_custom_order_status( $order_statuses ) {      
   $order_statuses['wc-custom-status'] = _x( 'Calculating Shipping', 'Order status', 'woocommerce' );       
   return $order_statuses;
}
 
add_filter( 'bulk_actions-edit-shop_order', 'get_custom_order_status_bulk' );
 
function get_custom_order_status_bulk( $bulk_actions ) {
   // Note: "mark_" must be there instead of "wc"
   $bulk_actions['mark_custom-status'] = 'Change status to calculating shipping';
   return $bulk_actions;
}


This solution was inspired by WooCommerce change BACS order status based on user roles seems to work but it changes the order status for shipping methods not specified here:

function bacs_order_payment_pending_order_status_shipping_method( $order_id ) {
    // Get $order object
    $order = wc_get_order( $order_id );
    
    // Is a WC_Order
    if ( is_a( $order, 'WC_Order' ) ) {
        // Get shipping method
        $shipping_method = $order->get_shipping_methods();
        
        // Shipping Methods
        $methods = (array) $shipping_method;
        
        // Shipping Methods to check
        $shipping_methods_to_check = array( 'flat_rate', 'request_shipping_quote' );
        
        // Compare
        $compare = array_diff( $methods, $shipping_methods_to_check );
        
        // Result is empty
        if ( empty ( $compare ) ) {
            if ( $order->get_payment_method() == 'bacs' && $order->has_status( 'on-hold' ) ) {
                $order->update_status( 'custom-status' );
            }
        }
    }
}
add_action( 'woocommerce_thankyou', 'bacs_order_payment_pending_order_status_shipping_method', 10, 1 );

The answer code from Change Woocommerce Order Status based on Shipping Method also works but I would like to specify several shipping methods.

UPDATE: In case you want to include logic to set another order status if the shipping methods are not found:

add_action( 'woocommerce_thankyou', 'bacs_order_payment_pending_order_status_shipping_method', 10, 1 );
function bacs_order_payment_pending_order_status_shipping_method( $order_id ) {
    // Get WC_Order object from the order Id
    $order = wc_get_order( $order_id );
    
    // Check that we get a WC_Order
    if ( is_a( $order, 'WC_Order' ) ) {
        // Shipping Methods to check
        $shipping_methods_to_check = array( 'flat_rate', 'request_shipping_quote' );
        
        $condition = $order->get_payment_method() == 'bacs' && $order->has_status( 'on-hold' );

        // Loop through shipping items (objects)
        foreach($order->get_shipping_methods() as $shipping_item ){
            // Check for matched defined shipping methods
            if( in_array( $shipping_item->get_method_id(), $shipping_methods_to_check ) && $condition ){
                $order->update_status( 'custom-status' ); // Change Order Status Custom 
            }
                else {$order->update_status( 'pending' ); // Change Order Status Pending
            }
        }
    }
}
1

There are 1 answers

3
LoicTheAztec On BEST ANSWER

Try the following instead (code is commented):

add_action( 'woocommerce_thankyou', 'bacs_order_payment_pending_order_status_shipping_method', 10, 1 );
function bacs_order_payment_pending_order_status_shipping_method( $order_id ) {
    // Get WC_Order object from the order Id
    $order = wc_get_order( $order_id );
    
    // Check that we get a WC_Order
    if ( is_a( $order, 'WC_Order' ) ) {
        // Shipping Methods to check
        $shipping_methods_to_check = array( 'flat_rate', 'request_shipping_quote' );
        
        $condition = $order->get_payment_method() == 'bacs' && $order->has_status( 'on-hold' );

        // Loop through shipping items (objects)
        foreach($order->get_shipping_methods() as $shipping_item ){
            // Check for matched defined shipping methods
            if( in_array( $shipping_item->get_method_id(), $shipping_methods_to_check ) && $condition ){
                $order->update_status( 'custom-status' ); // Change order status
                return; // Exit
            }
        }
    }
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

related: Change Woocommerce Order Status based on Shipping Method