提问人:Raymond 提问时间:10/27/2023 最后编辑:LoicTheAztecRaymond 更新时间:10/28/2023 访问量:147
只允许 WooCommerce 中高价产品的 BACS 付款
Allow only BACS payment for high priced products in WooCommerce
问:
我想限制买家仅通过 BACS 支付高价值商品,例如超过 99 件。我想出了以下代码,但它并没有隐藏卡支付。我不确定 WooPayments - 信用卡/借记卡方法中的值是否正确?card
$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;
}
答:
1赞
LoicTheAztec
10/28/2023
#1
以下代码将仅当购物车中有高价值商品(价格不超过 100 的产品)时,付款方式才会限制为 BACS(银行电汇):
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文件(或插件)中。经过测试并有效。
评论