问题
I've used the following code which works fine, it makes all orders default status to be on hold.
/**
* Auto Complete all WooCommerce orders.
*/
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id ) {
if ( ! $order_id ) {
return;
}
$order = wc_get_order( $order_id );
$order->update_status( 'on-hold' );
}
But I need to combine this with shipping/billing address. i.e. execute this code Only if the order's shipping or billing address is NOT UK or France. Customers from all other countries will be placed on hold as per this code, while UK & France orders get the default order status set by the payment geteway's settings.
Any help will be much appreciated.
回答1:
Try the following to exclude France and UK billing and shipping countries:
add_action( 'woocommerce_thankyou', 'country_based_auto_on_hold_orders' );
function country_based_auto_on_hold_orders( $order_id ) {
if ( ! $order_id ) {
return;
}
$order = wc_get_order( $order_id );
if ( ! ( in_array( $order->get_billing_country(), ['UK', 'FR'] ) || in_array( $order->get_shipping_country(), ['UK', 'FR'] ) ) ) {
$order->update_status( 'on-hold' );
}
}
Now using woocommerce_thankyou
hook, is the old way. To avoid multiple email notifications on status changes, use the following instead:
For all payment gateways except Cash on delivery (COD):
add_action( 'woocommerce_payment_complete_order_status', 'wc_auto_on_old_paid_order', 10, 3 );
function wc_auto_on_old_paid_order( $status, $order_id, $order ) {
if ( ! ( in_array( $order->get_billing_country(), ['UK', 'FR'] ) || in_array( $order->get_shipping_country(), ['UK', 'FR'] ) ) ) {
$status = 'on-hold'
}
return $status;
}
For Cash on delivery (COD) payments:
add_action( 'woocommerce_cod_process_payment_order_status', 'wc_auto_complete_cod_order', 10, 2 );
function wc_auto_complete_cod_order( $status, $order ) {
if ( ! ( in_array( $order->get_billing_country(), ['UK', 'FR'] ) || in_array( $order->get_shipping_country(), ['UK', 'FR'] ) ) ) {
$status = 'on-hold'
}
return $status;
}
Related: WooCommerce: Auto complete paid orders
来源:https://stackoverflow.com/questions/58218398/woocommerce-gateway-default-order-status-customization