WooCommerce: disable Klarna payment gateway by country or currency

I have for a customer met this scenario. Klarna Bank says "We do not offer Klarna in Canada or the US for your payment solution at the moment, that is why US/CA customers cannot pay with Klarna" In addition, Klarna says the following: "You have the following markets available on your account: AT, BE, DK, FR, DE, IT, NL, ES. In addition, we do NOT accept USD as currency, no matter where."

In this case, we would like to disable the Klarna Gateway for customers who do not live in the countries mentioned, i.e. not only Canada and the USA. Now I'm just using Klarna Bank as an example but regardless of the payment solution chosen, this is very common seen for all those who shop internationally that not all countries or currencies can be accepted.

I have made this little piece of code that makes the Klarna gateway only appear for the accepted countries and if the customer has not selected USD as currency. The code needs to be adapted to your solution, so it matches your problem. You need the slug name of the gateway solution you want to disable (here shown ['klarna']) and the slug is "klarna". In the two arrays, enter respectively the currencies that are NOT accepted and additionally the countries where the payment solution is accepted. The PHP code must be placed in the WordPress website theme (or child theme) functions.php file.


add_filter('woocommerce_available_payment_gateways', 'Klarna_CCO_woo_filter_gateways', 1);
function Klarna_CCO_woo_filter_gateways($gateway_list) {
   	if (!isset($gateway_list['klarna']) or is_admin()) return $gateway_list;
   $klarnaCountries = ['AT', 'BE', 'DK','FR','DE', 'IT','NL', 'ES']; // insert allowed countries
   $klarnaValuatas = ['USD']; // insert unallowed currencies
   if (in_array(get_woocommerce_currency(), $klarnaValuatas)) {unset($gateway_list['klarna']);} // unset if currency not allowed
   if (!in_array(WC()->customer->get_billing_country(), $klarnaCountries)) {unset($gateway_list['klarna']);} //unset gateway if not allowed country
   return $gateway_list;
}