Created
October 29, 2012 08:47
-
-
Save kloon/3972435 to your computer and use it in GitHub Desktop.
WooCommerce disable payment gateways based on customer country
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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' ); |
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' );
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
This is great but for my case I had to add a few lines:
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