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



如果选择的支付网关是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;
}

该解决方案的灵感来自WooCommerce基于用户角色更改BACS订单状态似乎有效,但它更改了此处未指定的运输方法的订单状态:

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

根据运输方式更改Wooccommerce订单状态中的答案代码也有效,但我想指定几种运输方式。

更新:如果您想包含逻辑来设置另一个订单状态,如果找不到运输方法:

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订单状态

相关内容

  • 没有找到相关文章

最新更新