简体   繁体   中英

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.

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.

I'm using the following code to disable authorize.net payment gateway when the cart total is less than $0:

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).

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. However, if you cart balance would be zero, Woocommmerce doesn't show the credit card form.

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. 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.

Hope this is helpful for someone, it was for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM