当订单金额低于 500 美元时,我试图隐藏银行账户转账付款方式,但代码不起作用。你能帮我找出错误吗?
add_filter( 'woocommerce_available_payment_gateways', 'show_hide_payment_methods_on_checkout' , 1 );
function show_hide_payment_methods_on_checkout( $available_gateways ) {
// Set minimum cart total
$minimum_order_total = 500;
// Get the order total
$order_total = WC()->cart->get_total( 'edit' );
echo 'Final Price: ' . $order_total;
// Check if the order total is less than or equal to the minimum
if ( $order_total <= $minimum_order_total ) {
// Check if 'bacs' payment gateway is available and unset it
if ( isset( $available_gateways['bacs'] ) ) {
unset( $available_gateways['bacs'] );
}
}
return $available_gateways;
}
尝试以下方法:
add_filter( 'woocommerce_available_payment_gateways', 'show_hide_bacs_payment_gateway' );
function show_hide_bacs_payment_gateway( $available_gateways ) {
// Only on checkout page
if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) )
return $available_gateways;
// Set max cart total
$targeted_total = 500;
// Check if the order total is less than or equal to the minimum for BACS payment
if ( WC()->cart->total <= $targeted_total && isset($available_gateways['bacs']) ) {
unset($available_gateways['bacs']);
}
return $available_gateways;
}
代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。