Search code examples
phpwordpresswoocommercepayment-methodarray-unset

Disable payment methods based on WooCommerce cart total


I tried this code below to hide/disable Credit/Debit card and Direct bank transfer payment method on Woo commerce(WordPress) when the checkout total == 400 but did not work. Please any idea on how to achieve this? Thank you so kindly.

function payment_gateway_disable_total_amount( $available_gateways ) {
global $woocommerce;

    if ( isset( $available_gateways['bacs'] ) && $woocommerce->cart->total == 400 ) {
        unset(  $available_gateways['bacs'] );
    }
    
    if ( isset( $available_gateways['youpay'] ) && $woocommerce->cart->total == 400 ) {
        unset(  $available_gateways['youpay'] );
    }
    return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_total_amount' );

Solution

  • Why using a fixed total? There is very few chances that any customer get speifically 400 as total. It should be "up to 400" instead, so something like if( $tolal >= 400 ).

    Also "Debit/Credit Cards" doesn't seem to be the right Payment method Id… See [this thread][1] to find out the right Payment method Id for "Debit/Credit Cards" payment gateway.

    Try the following (assuming that "Debit/Credit Cards" payment method id is correct):

    add_filter( 'woocommerce_available_payment_gateways', 'show_hide_payment_methods' );
    function show_hide_payment_methods( $available_gateways ) {
    
        if ( WC()->cart->total >= 400 ) {
            if ( isset($available_gateways['bacs']) ) {
                unset($available_gateways['bacs']);
            }
            if ( isset($available_gateways['Debit/Credit Cards']) ) {
                unset($available_gateways['Debit/Credit Cards']);
            }
        }
    
        return $available_gateways;
    }
    

    Code goes in functions.php file of the active child theme (or active theme). It should works.