简体   繁体   English

根据 Woocommerce 中的购物车总金额添加或删除支付网关

[英]add or remove payment gateways based on cart total amount in Woocommerce

I have a plugin that adds a fee when authorize.net is used as the payment gateway.我有一个插件,当使用authorize.net作为支付网关时会增加费用。

However, when I use a gift card that covers the entire purchase and brings the cart total down to $0, the order is still set to authorize.net and there's a fee when the customer isn't technically paying for anything.但是,当我使用涵盖整个购买的礼品卡并将购物车总额降至 0 美元时,订单仍设置为 authorize.net 并且当客户在技术上没有支付任何费用时会收取费用。

I'm using the following code to disable authorize.net payment gateway when the cart total is less than $0:当购物车总额小于 0 美元时,我使用以下代码禁用 authorize.net 支付网关:

function authorize_less($available_gateways) {
    $maximum = 0;
    if ( WC()->cart->total < $maximum ) {
    unset( $available_gateways['authorizenet'] );
    }
    return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'authorize_less' );



function authorize_more($available_gateways) {
    $maximum = 0;
    if ( WC()->cart->total > $maximum ) {
    unset( $available_gateways['cod'] );
    }
    return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'authorize_more' );

The function isn't working reliably.该功能无法可靠运行。 Either the credit card fee does not show up at all or requires a second refresh to appear or disappear based on what I do in the cart.要么信用卡费用根本不显示,要么需要根据我在购物车中的操作进行第二次刷新才能出现或消失。

The issue is probably more than the auto calculation and in the function itself.问题可能不仅仅是自动计算和函数本身。 there's probably a way to put it into one function rather than the two I have above, but I haven't had success with it可能有一种方法可以将它放入一个函数而不是我上面的两个函数中,但是我没有成功

Your both functions can be merged as follow and you are not targeting the zero cart total amount.您的两个功能可以如下合并,并且您的目标不是购物车总金额为零。

So you should try this:所以你应该试试这个:

add_filter( 'woocommerce_available_payment_gateways', 'conditional_available_payment_gateways' 20, 1 );
function conditional_available_payment_gateways( $available_gateways ) {
if( is_admin() ) return $available_gateways; // Only for frontend

    if ( WC()->cart->total <= 0 ) {
        unset( $available_gateways['authorizenet'] );
    } else {
        unset( $available_gateways['cod'] );
    }
    return $available_gateways;
}

Code goes in functions.php file of your active child theme (active theme).代码位于您的活动子主题(活动主题)的 functions.php 文件中。

Untested, but it could work.未经测试,但它可以工作。

Not sure if this helps anyone, but I need to do a credit card authorization for eWay which allows you to get a recurring payment token.不确定这是否对任何人有帮助,但我需要为 eWay 进行信用卡授权,这样您才能获得定期付款令牌。 However, if you cart balance would be zero, Woocommmerce doesn't show the credit card form.但是,如果您的购物车余额为零,Woocommmerce 不会显示信用卡表单。

Checking the source at includes/wc-template-functions.php , I found that the function woocommerce_checkout_payment applys a filter to see if payment is required.检查includes/wc-template-functions.php的源代码,我发现函数woocommerce_checkout_payment应用过滤器来查看是否需要付款。 Since this filter is never added, but just applied, I guess we can add it and it will get priority.由于从未添加过此过滤器,而只是应用了此过滤器,因此我想我们可以添加它并获得优先级。

add_filter( 'woocommerce_cart_needs_payment', function(){
    return true;
  }, 99 );

With this in place it always shows the credit card form, even for zero balance.有了这个,它总是显示信用卡形式,即使是零余额。 You can of course put more logic in the filter to decide to return true or false.您当然可以在过滤器中放置更多逻辑来决定返回 true 或 false。

Hope this is helpful for someone, it was for me.希望这对某人有帮助,这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据 WooCommerce 中的付款方式和购物车项目总计添加费用 - Add fee based on payment methods and cart items total in WooCommerce 根据 WooCommerce 购物车总数添加或删除特定购物车项目 - Add or remove specific cart Item based on WooCommerce cart total 禁用基于 WooCommerce 购物车总数的付款方式 - Disable payment methods based on WooCommerce cart total 根据 WooCommerce 购物车总数和月份范围添加或删除免费产品 - Add or remove a free product based on WooCommerce cart total and month range 如何在 WooCommerce 中的描述付款方式中显示总购物车订单金额 - How To Show Total Cart Order Amount In Desctiption Payment Method in WooCommerce 根据WooCommerce中的特定购物车总额添加费用 - Add fee based on specific cart total in WooCommerce 根据Woocommerce中用户总购买金额的自定义购物车通知 - Custom cart notice based on user total purchased amount in Woocommerce 允许根据 WooCommerce 中的购物车总数将特定产品添加到购物车 - Allow add to cart for specific products based on cart total in WooCommerce 根据 WooCommerce 购物车总数删除“继续结帐”按钮 - Remove "proceed to checkout" button based on WooCommerce cart total 如果在 Woocommerce 中应用了任何优惠券代码,则删除一些支付网关 - Remove some payment gateways if any coupon code is applied in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM