Difficulty: Medium
Here we will cover how to disable payment gateway for specific shipment methods. This comes very handy when you have cash on delivery payment gateway and you want to charge extra for that service by adding special shipment method that is only available on cash on delivery payment and hiding others.
If you need to disable payment gateways by country check out this guide.
Table of Contents
TLDR SECTION
*Don’t forget to replace <payment_gateway_id> and <shipping_method_id> in the code below.
**If you don’t know where to find it, just follow the guide (it takes 2 minutes) or look at our cheatsheet at the bottom of the page for payment gateways.
TLDR
File: Functions.php
add_filter( 'woocommerce_available_payment_gateways', 'woowiki_gateway_disable_shipping' );
function woowiki_gateway_disable_shipping( $available_gateways ) {
if ( ! is_admin() ) {
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if (isset( $available_gateways['<payment_gateway_id_1>'] ) && 0 === strpos( $chosen_shipping, '<shipping_method_id>' ) ) {
unset( $available_gateways['<payment_gateway_id>'] );
} //If statement for disabling payment gateway
}
return $available_gateways;
}
Finding Payment Gateway Id
In order to disable payment gateway for specific shipment methods we need to find payment gateway id that we want to disable for specific shipment method. Navigate to admin interface and then under WooCommerce select Settings.
After navigating there you will see a window like this one below. In there you will need to press right click on one of those payment gateways and select inspect element (on some browsers it just says inspect).
After clicking inspect element a windows like this one below will open and there we will find id for all of our payment methods. I suggest to write them down cause we will need them later in our next step.
Finding shipment method ID
Okay, now that we got Payment ID we need to find out shipment method ID. For that you need to navigate to yours checkout page.
On there right click the shipment method you want to get ID for (Like shown in the picture belom). Then in the menu click Inspect / Inspect element.
Same as with payment gateway IDs I suggest to write down shipment method IDs cause we will need them in our next step.
Disabling payment gateway for specific shipment methods
Okay, now that we got Payment ID and shipment ID we need to go to the admin interface and then go to Appearance and then under Appearance select Theme Editor
After navigating there you will see a windows like this one below. In there select Theme Functions (functions.php) and then at the end put this code.
*Don’t forget to replace <payment_gateway_id> with payment gateway id
**Don’t forget to replace <shipping_method_id> with shipment method id
***In case you need to disable multiple you would just add more if statements. We have examples below
add_filter( 'woocommerce_available_payment_gateways', 'woowiki_gateway_disable_shipping' );
function woowiki_gateway_disable_shipping( $available_gateways ) {
if ( ! is_admin() ) {
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if (isset( $available_gateways['<payment_gateway_id_1>'] ) && 0 === strpos( $chosen_shipping, '<shipping_method_id>' ) ) {
unset( $available_gateways['<payment_gateway_id>'] );
} //If statement for disabling payment gateway
}
return $available_gateways;
}
Editing the code
There isn’t much you can do with this one, except maybe disabling more than one payment gateway for specific shipping method. In order to achieve that we would just add more if statements to our function and that would be it. Here is an example below. Pretty simple isn’t it.
add_filter( 'woocommerce_available_payment_gateways', 'woowiki_gateway_disable_shipping' );
function woowiki_gateway_disable_shipping_326( $available_gateways ) {
if ( ! is_admin() ) {
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if (isset( $available_gateways['cod'] ) && 0 === strpos( $chosen_shipping, 'flexible_shipping_single:1' ) ) {
unset( $available_gateways['cod'] );
} //This if statement disables cod for flexible_shipping_single:1
if (isset( $available_gateways['corvuspay'] ) && 0 === strpos( $chosen_shipping, 'flexible_shipping_single:1' ) ) {
unset( $available_gateways['corvuspay'] );
} //This if statement disables corvuspay for flexible_shipping_single:1
}
return $available_gateways;
}
Here is one more example where we disable cod for 1st shipment method but we keep corvuspay. But for the 2nd shipment method we disable corvuspay and keep cod.
add_filter( 'woocommerce_available_payment_gateways', 'woowiki_gateway_disable_shipping' );
function woowiki_gateway_disable_shipping( $available_gateways ) {
if ( ! is_admin() ) {
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if (isset( $available_gateways['cod'] ) && 0 === strpos( $chosen_shipping, 'flexible_shipping_single:1' ) ) {
unset( $available_gateways['cod'] );
} //This if statement disables cod for flexible_shipping_single:1
if (isset( $available_gateways['corvuspay'] ) && 0 === strpos( $chosen_shipping, 'flexible_shipping_single:2' ) ) {
unset( $available_gateways['corvuspay'] );
} //This if statement disables corvuspay for flexible_shipping_single:2
}
return $available_gateways;
}
Payment gateway ID Cheatsheet
corvuspay – CorvusPay
cod – Cash on delivery
bacs – Direct bank transfer
cheque – Check payments
paypal – PayPal Standard
erste-kekspay-woocommerce -KEKS Pay