我想限制买家仅通过 BACS 支付高价值商品,例如商品超过 99。我想出了以下代码,但它并没有隐藏卡付款。我不确定
card
是否是 WooPayments - 信用卡/借记卡方法的 $available_gateways['card']
中的正确值?
如何纠正?
functions.php
//////////// Restrict payment option to be BACS for high value items
add_filter('woocommerce_available_payment_gateways', 'restrict_bacs_for_high_value_items', 99, 1);
function restrict_bacs_for_high_value_items( $available_gateways ) {
global $product;
if ( is_admin() ) return $available_gateways; // Only on frontend
$product_price = round($product->price);
if ( isset($available_gateways['card']) && ($product_price > 99) ) {
unset($available_gateways['card']);
}
return $available_gateways;
}
以下代码会将付款方式限制为 BACS (银行电汇),前提是购物车中有高价值商品(价格不超过 100 的产品):
add_filter('woocommerce_available_payment_gateways', 'restrict_bacs_for_high_value_items', 99, 1);
function restrict_bacs_for_high_value_items( $available_gateways ) {
// Only on frontend and if BACS payment method is enabled
if ( ! is_admin() && isset($available_gateways['bacs']) ) {
// Loop through cart items
foreach ( WC()->cart->get_cart() as $item ) {
// If an item has a price up to 100
if ( $item['data']->get_price() >= 100 ) {
// Only BACS payment allowed
return ['bacs' => $available_gateways['bacs']];
}
}
}
return $available_gateways;
}
代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。
(为了问题作者的利益,代表答案作者重新发布以暂时恢复它。)
仅当购物车中有高价值商品时,以下内容才会将付款方式限制为 BACS (> 99):
add_filter('woocommerce_available_payment_gateways', 'restrict_bacs_for_high_value_items', 99, 1);
function restrict_bacs_for_high_value_items( $available_gateways ) {
// Only on frontend and if BACS payment method is enabled
if ( ! is_admin() && isset($available_gateways['bacs']) ) {
// Loop through cart items
foreach ( WC()->cart->get_cart() as $item ) {
// If an item has a price up to 100
if ( $item['data']->get_price() >= 100 ) {
// Only BACS payment allowed
return ['bacs' => $available_gateways['bacs']];
}
}
}
return $available_gateways;
}
代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。