Skip to content

Instantly share code, notes, and snippets.

@bagerathan
Last active August 21, 2021 04:56
Show Gist options
  • Save bagerathan/a6fed4d96238fdcd63a93973443265b7 to your computer and use it in GitHub Desktop.
Save bagerathan/a6fed4d96238fdcd63a93973443265b7 to your computer and use it in GitHub Desktop.
[Disable gateway for certain country] #woo
add_filter( 'woocommerce_available_payment_gateways', 'rudr_gateway_by_country' );
function rudr_gateway_by_country( $gateways ) {
if( is_admin() ) {
return $gateways;
}
if( is_wc_endpoint_url( 'order-pay' ) ) { // Pay for order page
$order = wc_get_order( wc_get_order_id_by_order_key( $_GET[ 'key' ] ) );
$country = $order->get_billing_country();
} else { // Cart page
$country = WC()->customer->get_billing_country();
}
if ( 'MC' === $country ) {
if ( isset( $gateways[ 'paypal' ] ) ) {
unset( $gateways[ 'paypal' ] );
}
}
return $gateways;
}
//for multiple countries
f ( 'MC' === $country || 'MY' === $country ) {
//unset gateway
}
//or
$countries = array( // array of unsupported countries
'MC',
'MY',
// more countries here...
);
if( in_array( $country, $countries ) ) {
//unset gateway
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment