WooCommerce 根据特定运输方式更改 BACS 订单的状态

WooCommerce change status for BACS orders based on specific shipping methods

如果选择的支付网关是 BACS,我如何将特定运输方式的订单状态从暂停更改为我自己的自定义状态?

这是我添加自己的自定义状态的方式:

// 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;
}


此解决方案的灵感来自 似乎可行,但它会更改此处未指定的运输方式的订单状态:

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 );

来自的答案代码也可以,但我想指定几种送货方式。

更新: 如果您希望在未找到送货方式时包含设置其他订单状态的逻辑:

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
            }
        }
    }
}

改为尝试以下操作(代码已注释):

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
            }
        }
    }
}

代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。

相关:根据运输方式更改 Woocommerce 订单状态