Hide WooCommerce BACS payment if cart total is less than a specific amount

57 Views Asked by At

I'm trying to hide the Bank Account Transfer payment method when the order is less than $500 but the code is not working. Can you help me to find the error?

 add_filter( 'woocommerce_available_payment_gateways', 'show_hide_payment_methods_on_checkout' , 1 );

function show_hide_payment_methods_on_checkout( $available_gateways ) {
    // Set minimum cart total
    $minimum_order_total = 500;

    // Get the order total
    $order_total = WC()->cart->get_total( 'edit' );
    echo 'Final Price: ' . $order_total;
    // Check if the order total is less than or equal to the minimum
    if ( $order_total <= $minimum_order_total ) {
        // Check if 'bacs' payment gateway is available and unset it
        if ( isset( $available_gateways['bacs'] ) ) {
            unset( $available_gateways['bacs'] );
        }
    }

    return $available_gateways;
}
1

There are 1 best solutions below

1
LoicTheAztec On

Try the following instead:

add_filter( 'woocommerce_available_payment_gateways', 'show_hide_bacs_payment_gateway' );
function show_hide_bacs_payment_gateway( $available_gateways ) {
    // Only on checkout page
    if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) )
        return $available_gateways;

    // Set the targeted cart total
    $targeted_total = 500;

    // Check if the order total is less than or equal to the minimum for BACS payment
    if ( WC()->cart->total <= $targeted_total && isset($available_gateways['bacs']) ) {
        unset($available_gateways['bacs']);
    }
    return $available_gateways;
}

Code goes in functions.php file of your child theme (or in a plugin). Tested and works.