How to Disable a Payment Gateway for a Specific Shipping Method in WooCommerce

How to Disable a Payment Gateway for a Specific Shipping Method in WooCommerce
On this page
  1. Why Restrict a Gateway by Shipping Method
  2. Step 1: Check the Session for the Chosen Shipping Method
  3. Troubleshooting
  4. Frequently Asked Questions
  5. Conclusion

You can disable a payment gateway for a specific shipping method in WooCommerce by reading the customer’s chosen shipping method from the session and unsetting the target gateway whenever it matches — commonly used to block Cash on Delivery when Local Pickup is selected.

Why Restrict a Gateway by Shipping Method

  • Cash on Delivery often makes more sense for pickup orders than for shipped orders, or vice versa depending on your process
  • Certain shipping methods carry risk profiles that only work with prepaid, verified payment methods
  • You want to steer customers toward the right payment option based on how their order will actually be fulfilled

Step 1: Check the Session for the Chosen Shipping Method

PHP
/**
 * Disable Cash on Delivery when Local Pickup is the chosen shipping method
 * Add to your child theme's functions.php
 */
add_filter( 'woocommerce_available_payment_gateways', 'disable_gateway_for_pickup_shipping' );

function disable_gateway_for_pickup_shipping( $available_gateways ) {
    if ( is_admin() || ! WC()->session ) {
        return $available_gateways;
    }

    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    $chosen_shipping = isset( $chosen_methods[0] ) ? $chosen_methods[0] : '';

    if ( isset( $available_gateways['cod'] ) && 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
        unset( $available_gateways['cod'] );
    }

    return $available_gateways;
}
Disable a payment gateway for a specific shipping method in WooCommerce: the page before adding the code
Before
Disable a payment gateway for a specific shipping method in WooCommerce: the page after adding the code
After

Troubleshooting

The Gateway Doesn’t Update Immediately After Changing Shipping Method

Checkout normally refreshes payment options via Ajax when the shipping method changes. If it seems stuck on an old value, confirm your theme hasn’t disabled the default checkout update trigger.

The Check Targets the Wrong Shipping Rate

Shipping method IDs include an instance number specific to your zone setup (like local_pickup:5). The strpos() check above matches any instance starting with “local_pickup” — if you need to target one exact rate instead, compare the full string with === instead.

Frequently Asked Questions

Can I use this same technique to disable a payment gateway for a specific shipping method combined with a customer role?

Yes, add a role check alongside the shipping method check inside the same function so the restriction only applies to certain customer types.

Can I disable a gateway for multiple shipping methods?

Yes, change the single strpos() check to a loop or an array comparison covering each shipping method prefix you want to match.

Can I do the reverse — only allow a gateway for one shipping method?

Yes, invert the logic so every gateway except the one you want is removed when the target shipping method is not selected.

Does this work with multiple shipping packages in one cart?

The example checks only the first package’s chosen method. For multi-package carts, loop through the full $chosen_methods array and decide how you want to handle mixed selections.

Conclusion

If you need to disable a payment gateway for a specific shipping method in WooCommerce, reading the chosen shipping method from the session is the key step, letting you pair the right payment options with the right fulfillment method. Test with multiple shipping zones active, since rate IDs and instance numbers can vary by zone configuration.

See also How to Disable a Payment Method for a Specific Product Category in WooCommerce.

Prefer no code? All-in-One Utilities Woo Add-on handles this kind of change from a settings screen. Its Conditional payment methods guide also shows how.

Leave a Reply

Schedule a call