tadrik Free store check

Guides / WooCommerce

Hide payment methods by shipping method in WooCommerce

You want cash on delivery only when the customer collects the order, or bank transfer only for a freight shipping method. WooCommerce lets you enable a payment gateway, but not tie it to the shipping method the customer picked. Here are two ways to do it.

What WooCommerce does itself

Under WooCommerce > Settings > Payments each gateway is on or off. A few gateways have their own option: Cash on delivery has "Enable for shipping methods", which limits it to the methods you tick. Other gateways have no such setting, so for them the shipping method cannot be used as a condition without extra code.

Option 1: a code snippet

Add this to a small custom plugin or your child theme's functions.php. It reads the shipping method the customer chose and removes cash on delivery unless it is local pickup.

add_filter( 'woocommerce_available_payment_gateways', function ( $gateways ) {
    if ( is_admin() || ! WC()->session ) {
        return $gateways;
    }
    $chosen = (array) WC()->session->get( 'chosen_shipping_methods' );
    if ( ! $chosen ) {
        return $gateways;
    }
    $is_pickup = false;
    foreach ( $chosen as $id ) {
        if ( 0 === strpos( $id, 'local_pickup' ) ) {
            $is_pickup = true;
        }
    }
    if ( ! $is_pickup ) {
        unset( $gateways['cod'] );
    }
    return $gateways;
} );

Notes on the snippet:

  • The chosen shipping method id looks like flat_rate:3: the method, a colon, the instance id. Compare the start of it, as the snippet does.
  • The payment list must refresh when the customer changes the shipping method. The classic checkout does this; check that yours does after you add the code, in both checkouts.
  • The gateway id is cod for cash on delivery, bacs for bank transfer and cheque for cheque. Other plugins use their own ids.
  • Keep a copy of the file before you edit it: a mistake in a snippet can break the site.

Option 2: a rule in Tadrik Checkout Rules

Tadrik Checkout Rules is a free plugin on WordPress.org. Under WooCommerce > Checkout Rules, add a rule with the action "Hide payment methods", pick Cash on delivery, and add the condition "Shipping method picked".

Hide Cash on delivery when the shipping method picked is Flat rate.

Add more conditions to the same rule when you need them: only for one country, only above a cart total, or only for guests. The plugin has the condition "Shipping method picked" for payment rules.

Two safety points. If a rule would leave a customer with no way to pay, the plugin shows every method instead and warns you on the rules page. And the Test a cart tab lets you build a cart and see which payment methods are offered and which rule hid the rest, without placing an order.

Which should you pick?

  • Snippet: one simple behaviour for the whole store, and you are comfortable with code.
  • Plugin: several rules, other conditions mixed in, or you want to check a cart and switch a rule off without touching code.

Test it before customers do

Put a cart through checkout twice: once with the shipping method that should allow the payment method, once with the one that should not. Switch from one to the other on the same page and check that the payment list changes. Do it on a phone too.

Common questions

Can WooCommerce show a payment method only for a certain shipping method? Not by itself. Payment gateways have no setting tied to the shipping method. You need a snippet or a plugin.

Why does my payment list not change when the customer switches shipping method? The checkout has to refresh the payment list when the shipping choice changes. The classic checkout does this with its update_checkout event; the block checkout re-reads the available gateways as the cart changes. Test both ways of changing it on your own store.

Does it work with the block checkout? Checkout Rules applies its rules to the block checkout and the classic one. For the snippet, test a cart on your own checkout: block checkout support for gateway filtering depends on the gateway plugin.

Can I combine it with other conditions? Yes. In Checkout Rules, add more conditions to the same rule, such as country, cart total or customer role. In a snippet, add the checks to the same function.

Related: show payment methods by user role, disable cash on delivery for some postcodes or order totals, and hide other shipping methods when free shipping is available.

Tadrik Checkout Rules

Choose which payment methods each shipping method allows. Free on WordPress.org.

See Tadrik Checkout Rules