There are different reasons why someone wants to specifically enable or disable specific payment gateways for some shipping methods. In this post I will show you how you can enable or disable payment gateways based on the shipping method with a code snippet.

Disable payment gateways for shipping methods

For this example I will disable the ‘BACS’ payment gateway for the ‘local delivery’ shipping method. To get started you’d need to get the ID or both the payment gateway and the shipping method. To get these IDs you can go to the settings section of the given payment gateway, the URL should end with something like &section=wc_gateway_paypal. The ID of the payment gateway here is paypal (‘&section’ value, with ‘wc_gateway_’ omitted). For the shipping rate this is very similar, the URL ends with &section=wc_shipping_local_delivery where ‘local_delivery’ is the shipping method ID.

Next we would need to add the following code snippet to remove the given payment gateway when the given shipping rate has been selected.

In the code snippet you will see on line 10 that we are checking if the local_delivery shipping method has been selected, and if that is the case then on line 13 the bacs payment gateway will be disabled. You can change these values to the IDs that you’ve retrieved before.

If you want to disable multiple payment gateways for a specific shipping method, you can copy line 13 and add those extra payment gateways.

Enable payment gateways for shipping methods

The opposite, enabling payment gateways only for specific shipping methods is very similar. We still need to get the IDs of the payment gateway/shipping method. In this example I will only enable the ‘cash on delivery’ payment gateway when the ‘local delivery’ shipping method has been selected. I know that COD also has a option available for this, but for some shipping plugins, like WooCommerce Advanced Shipping, which add multiple shipping rates under one shipping method the shipping rates cannot be selected from the drop down list in the Cash On Delivery gateway. Via this way specific shipping rates (which most likely do not have the same shipping ID as mentioned above) can also be used to enable the Cash On Delivery gateway for.

(for WooCommerce Advanced Shipping users that are looking for the shipping ID, when looking in the shipping rate in detail, you can find the numeric ID in the URL ?post=33696)

In the code snippet above you notice that there is very little difference then the ‘disable payment gateway’ code. The difference here is a exclamation mark on line 10 that makes sure that when the Cash On Delivery is always removed when ‘Local Delivery’ is not the chosen shipping method.

Closing words

These code snippets hopefully get you started on modifying your payment gateway availability. If you have any questions, don’t hesitate to ask!

I would love to hear from you if this was useful in any way, have feedback, missing something that you’d love to see or just a Thank you 🙂

Jeroen Sormani

I'm a professional WordPress plugin developer on a mission to create the best plugins for my clients. I'm specialised in developing general WordPress, WooCommerce and Easy Digital Downloads plugins.

Interested in talking about a WordPress project? Get in touch!

Follow me on Twitter

48 thoughts on “Enable/Disable payment gateways for specific shipping methods

Kate Gale November 8, 2017 at 8:04 am

Can this code be applied to a shipping class instead of shipping methods?

Thanks

Jeroen November 8, 2017 at 9:41 am

Hi Kate,

Sure, you can use this code as a basis to start with and customize it to fit for shipping classes.

Simon February 2, 2018 at 4:18 pm

Hi Jeroen,

Thank you for the code it works. Can you please tell me how to adjust the code to make it work for more than one country. Unset more than one gateway is described:

// Remove bank transfer payment gateway
unset( $gateways[‘bacs’] );
unser…..

But if I use the same system for unset more than one sipping rate, it does not work e.g:

// When ‘local delivery’ has been chosen as shipping rate
if ( in_array( ‘flat_rate:9’, $chosen_shipping_rates ) ) :
// When ‘local delivery’ has been chosen as shipping rate
if ( in_array( ‘flat_rate:6’, $chosen_shipping_rates ) ) :

Thank you

Arjan April 6, 2018 at 2:38 pm

Hi Jeroen,

i am using your snippet to my full satisfaction.
Question, can I also limit / filter payment methods based on shipping/billing country?
If so, what would the code look like?

Alex September 10, 2019 at 4:59 am

Hi Jeroen,

How can I hide specific payment gateways if billing and shipping address are not same (different zip code)

Jeroen Sormani September 10, 2019 at 9:25 am

Hi Alex,

It is possible to use either of the code snippets as a basis for a customization like that.
You’d just have to get and use the customer shipping/billing details for this.

Unfortunately I don’t have a code snippet for this ready to go, so some digging in would be required 😉

Marian December 9, 2021 at 8:51 am

Hi!

Thanks for the code. Your current code returns error in WooCommerce admin (at leat in the customers section). Right at the beginning of the function, you should include the code below.

if( is_admin() || is_wc_endpoint_url(‘order-pay’) ) return $gateways;

Jeroen Sormani December 9, 2021 at 1:45 pm

Hi Marian,

So far I’m not able to replicate this, where/when exactly is there an error occuring?

Cheers,
Jeroen

Leave a Reply