donglu9445 2015-11-17 15:05
浏览 38

如果价格超过500,则禁用BACS支付网关

I'm trying to disable the bacs gateway if the customers cart total is over £500.

I have this code here which I've placed into my storefront functions.php file

add_filter('woocommerce_available_payment_gateways','filter_gateways',1);
function filter_gateways($gateways){
global $woocommerce;

$min_cart_total = 500;

$cart_total = ($woocommerce->cart->get_cart_total());
$cart_total = ereg_replace("[^0-9]", "", $cart_total)/100;

if ($cart_total > $min_cart_total){
    unset($gateways['bacs']);
}

return $gateways;
}

But the options for bacs still shows up at the end even if my cart total is £250 for example.

Is there anything I'm doing wrong here?

  • 写回答

1条回答 默认 最新

  • duanmiao6695 2015-11-17 19:04
    关注

    I'm not sure about your ereg_replace code, but I think you should for sure be using a later priority:

    add_filter('woocommerce_available_payment_gateways','filter_gateways', 20);
    function filter_gateways($gateways){
    
    $min_cart_total = 500;
    
    $cart_total = WC()->cart->cart_contents_total; // the total without wc_price formatting
    
    if ($cart_total > $min_cart_total){
        unset($gateways['bacs']);
    }
    
    return $gateways;
    }
    

    Your code was running first, and the Woo code was probably running later and adding the gateway back to the available gateways.

    Doesn't impact anything yet, but the global $woocommerce is deprecated in favor of WC().

    评论

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害