Skip to content

Instantly share code, notes, and snippets.

@kloon
Created October 29, 2012 08:47
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kloon/3972435 to your computer and use it in GitHub Desktop.
Save kloon/3972435 to your computer and use it in GitHub Desktop.
WooCommerce disable payment gateways based on customer country
// Disable gateway based on country
function payment_gateway_disable_country( $available_gateways ) {
global $woocommerce;
if ( isset( $available_gateways['ccavenue'] ) && $woocommerce->customer->get_country() <> 'IN' ) {
unset( $available_gateways['ccavenue'] );
} else if ( isset( $available_gateways['paypal'] ) && $woocommerce->customer->get_country() == 'IN' ) {
unset( $available_gateways['paypal'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );
@one2love
Copy link

Howdy Kloon
Wanted to know if you had any success with this here snippet?

@kloon
Copy link
Author

kloon commented Feb 24, 2013

@one2love yes this works 100% you just need to adjust it to suite your needs.

@alexalloy
Copy link

Hello thanks for the code. Is there any chance you would know how I could do the reverse: For example: to only ALLOW the Cash On Delivery gateway when the customer is in a specific country?

Cheers.

@agarwalc
Copy link

agarwalc commented Oct 9, 2013

Great one!

How can I disable COD gateway if the customer is checking out with atleast 1 downloadable product in his cart?

@Frekisunr
Copy link

This is great but for my case I had to add a few lines:

function payment_gateway_allow_germany( $available_gateways ) {
    if ( !is_admin() ){
    global $woocommerce;
  $country = !empty($woocommerce->customer->get_shipping_country()) ? $woocommerce->customer->get_shipping_country() : $woocommerce->customer->get_country();
        if ( isset( $available_gateways['cash_on_delivery'] ) && $country <> 'DE' ) {
            unset( $available_gateways['cash_on_delivery'] );
        }
    }
    return $available_gateways;
 }
 add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_allow_germany' );

First I have to check if I'm not in admin area and the second one you have to test shipping countrycode if it's set, if not just user countrycode:
$country = !empty($woocommerce->customer->get_shipping_country()) ? $woocommerce->customer->get_shipping_country() : $woocommerce->customer->get_country();

Hope that's helpfull for others

@johnymas
Copy link

johnymas commented Apr 17, 2017

Thanks Frekisunr

Due to ERROR Call to a member function get_country() on null I used your code and it works great and without error

function payment_gateway_allow_slovenian( $available_gateways ) {
    if ( !is_admin() ){
    global $woocommerce;
  $country = !empty($woocommerce->customer->get_country()) ? $woocommerce->customer->get_country() : $woocommerce->customer->get_country();
        if ( isset( $available_gateways['bacs'] ) && $country <> 'SI' ) {
            unset( $available_gateways['bacs'] );
        }
$country = !empty($woocommerce->customer->get_country()) ? $woocommerce->customer->get_country() : $woocommerce->customer->get_country();
        if ( isset( $available_gateways['cod'] ) && $country <> 'SI' ) {
            unset( $available_gateways['cod'] );
        }
    }
    return $available_gateways;
 }
 add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_allow_slovenian' );

@jumjum95
Copy link

Hey Johnymas, I used your code, works fine! But when there's only one payment gateway to choose from, the radio button and space between the header and gateway disappears.. Is there a way to fix that?
skarmavbild 2017-07-27 kl 09 12 02
skarmavbild 2017-07-27 kl 09 12 15

@aksthelion
Copy link

Hello, can anyone let me know what is the value to pass to $available_gateways[ ] to disable card payment option?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment