问题描述
我们在WooCommerce商店中提供BACS(银行转帐)作为付款方式。 如果有人使用该方法:
- 该产品的库存减少
- 订单被保留48小时
我们面临的问题是,有些人利用了这种优势,然后使用这种方法来储备产品。他们等待订单取消,然后再次执行,只要有钱,他们就会实际付款。
作为二手商店,这当然不是我们想要的行为。
这样,如果您想使用BACS付款并且您的电子邮件已经有一个待处理的订单,它将返回一个错误,您将无法完成结帐。 当然,人们可以使用不同的电子邮件,但是它变得越来越复杂。
感谢您的帮助。
解决方法
已更新-使用帐单电子邮件添加的来宾。
以下非常简单的挂钩函数将检查当前客户是否有任何“保留”的BACS订单,如果是这样,如果当前订单正在使用BACS付款方式,它将显示一条错误消息,避免结帐:
add_action( 'woocommerce_checkout_process','action_wc_checkout_process_callback' );
function action_wc_checkout_process_callback() {
if( is_user_logged_in() ) {
// Get customer "on-hold" orders
$orders = (array) wc_get_orders(['limit' => - 1,'customer_id' => get_current_user_id(),'status' => 'on-hold','return' => 'ids']);
} elseif ( isset($_POST['billing_email']) && ! empty($_POST['billing_email']) ) {
// Get customer "on-hold" orders
$orders = (array) wc_get_orders(['limit' => - 1,'customer' => sanitize_email($_POST['billing_email']),'return' => 'ids']);
}
// Check if the current customer has any "on-hold" BACS order and if current order is using BACS as payment method
if ( isset($orders) && count($orders) > 0 && isset($_POST['payment_method']) && $_POST['payment_method'] === 'bacs' ) {
// Display an error notice (and avoid checkout)
wc_add_notice( __( "You have already one order awaiting payment and you can have only one at the time.","woocommerce" ),'error' );
}
}
代码进入活动子主题(或活动主题)的functions.php文件中。经过测试,可以正常工作。