phpwordpresswoocommerceorderspayment-method

Allow only BACS payment for high priced products in WooCommerce


I want to restrict buyers to pay via BACS only for high value items e.g. more than 99. I've come up with following code but it doesn't hide card payment. I'm not sure if card is the correct value in $available_gateways['card'] for WooPayments - Credit card / debit card method?

How to correct it?

functions.php

//////////// Restrict payment option to be BACS for high value items
add_filter('woocommerce_available_payment_gateways', 'restrict_bacs_for_high_value_items', 99, 1);
function restrict_bacs_for_high_value_items( $available_gateways ) {
    global $product;

    if ( is_admin() ) return $available_gateways; // Only on frontend

    $product_price = round($product->price);

    if ( isset($available_gateways['card']) && ($product_price > 99) ) {
        unset($available_gateways['card']);
    }
    return $available_gateways;
}

Solution

  • The following code will restrict payments methods to BACS (bank wire) only if a high value item is in cart (product with a price up to 100):

    add_filter('woocommerce_available_payment_gateways', 'restrict_bacs_for_high_value_items', 99, 1);
    function restrict_bacs_for_high_value_items( $available_gateways ) {
        // Only on frontend and if BACS payment method is enabled
        if ( ! is_admin() && isset($available_gateways['bacs']) ) {
            // Loop through cart items
            foreach ( WC()->cart->get_cart() as $item ) {
                // If an item has a price up to 100
                if ( $item['data']->get_price() >= 100 ) {
                    // Only BACS payment allowed
                    return ['bacs' => $available_gateways['bacs']]; 
                }
            }
        }
        return $available_gateways;
    }
    

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