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
§ion=wc_gateway_paypal. The ID of the payment gateway here is
paypal (‘§ion’ value, with ‘wc_gateway_’ omitted). For the shipping rate this is very similar, the URL ends with
§ion=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
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.
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 🙂