问题
In WooCommerce any order placed with the BACS (direct bank transfer) is set to "on-hold"
.
How would one go about changing this automatically to processing?
I wan't it to work inside the functions.php
I have the following code but that doesn't work:
add_filter( 'woocommerce_payment_complete_order_status', 'rfvc_update_order_status', 10, 2 );
function rfvc_update_order_status( $order_status, $order_id ) {
$order = new WC_Order( $order_id );
if ( 'on-hold' == $order_status && 'on-hold' == $order->status ) {
return 'processing';
}
return $order_status;
}
Any help would be great!
回答1:
Update (added a version for woocommerce 3+ at the end)
It seems that woocommerce_payment_complete_order_status
action hook doesn't trigger with BACS payment method.
Based on this thread, 'woocommerce_thankyou'
action hook does the job:
add_action( 'woocommerce_thankyou', 'bacs_order_payment_processing_order_status', 10, 1 );
function bacs_order_payment_processing_order_status( $order_id ) {
if ( ! $order_id ) {
return;
}
// Get an instance of the WC_Order object
$order = new WC_Order( $order_id );
if ( ( get_post_meta($order->id, '_payment_method', true) == 'bacs' ) && ('on-hold' == $order->status || 'pending' == $order->status) ) {
$order->update_status('processing');
} else {
return;
}
}
Code goes in function.php file of your active child theme (or active theme). tested and works.
For woocommerce 3+ versions:
Here we use the similar composite hook woocommerce_thankyou_{$order->get_payment_method()}:
add_action( 'woocommerce_thankyou_bacs', 'bacs_order_payment_processing_order_status', 10, 1 );
function bacs_order_payment_processing_order_status( $order_id ) {
if ( ! $order_id ) {
return;
}
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
if ( in_array( $order->get_status(), array('on-hold', 'pending') ) ) {
$order->update_status('processing');
} else {
return;
}
}
Code goes in function.php file of your active child theme (or active theme). tested and works.
回答2:
Try changing the code to this:
function rfvc_update_order_status( $order_status, $order_id ) {
$order = new WC_Order( $order_id );
if ( 'on-hold' == $order_status && 'on-hold' == $order->status ) {
$order->update_status('processing', 'order_note');
}
return $order_status;
}
The key change here is this:
$order->update_status('processing', 'order_note');
You can add order note too if you prefer.
回答3:
There is a new filter which will allow you to set the status when BACS 'payment' is processed.
/**
* Change the default status when BACS 'payment' is processed.
*
* @see WC_Gateway_BACS::process_payment()
* woocommerce/includes/gateways/bacs/class-wc-gateway-bacs.php:362
* @since Mar 8, 2018
* @link https://github.com/woocommerce/woocommerce/blob/750fda3b1b55c55645f626d3873d956282e3ac1b/includes/gateways/bacs/class-wc-gateway-bacs.php#L364
*
* @filter woocommerce_bacs_process_payment_order_status
* @priority 10
* @args 2
*
* @param string $status Status to filter. Default 'on-hold'.
* @param WC_Order $order
* @return string New status 'processing'.
*/
add_filter( 'woocommerce_bacs_process_payment_order_status', function( $status = 'on_hold', $order = null ) {
return 'processing';
}, 10, 2 );
来源:https://stackoverflow.com/questions/36597663/woocommerce-change-order-status-bacs-processing