1
votes

On the checkout page, we've a currency switcher for USD, EUR, INR, GBP currencies.

However, out of 3, only 1 gateway supports all currencies, & 2 supports INR.

So, I need to hide other two gateways, if someone selects USD, EUR & GBP

Can we use selected currency & do the needed function?

TIA

3

3 Answers

1
votes

I use this to remove gateways if a particular product is in the cart:

add_filter('woocommerce_available_payment_gateways','filter_gateways',1);
function filter_gateways($gateways){
  global $woocommerce;
  foreach ($woocommerce->cart->cart_contents as $key => $values ) {
  $product_ = array(1063);
  if(in_array($values['product_id'],$product_)){
   unset($gateways['paypal']);
   break;
 }}
 return $gateways;
}

I dont know what your currency switcher is to get any variable from to modify the woocommerce_available_payment_gateways filter though.

0
votes

Okay. found a solution. Giving details below, if anyone stumbles here.

The "WooCommerce Currency Switcher" plugin changes the shop currency & stores using session / transient method. Not just that, it also adds a body class for each selected currency.

e.g.: currency-usd or currency-eur or currency-gbp

This is what is possible simply using CSS

.currency-usd .payment_method_instamojo, .currency-usd .payment_method_paynimo, .currency-eur .payment_method_instamojo, .currency-eur .payment_method_paynimo, .currency-gbp .payment_method_instamojo, .currency-gbp .payment_method_paynimo {display:none}

This works as expected w/o writing a separate function. Hope this helps.

0
votes

This is working code and I have implemented it on [www.edupediapublications.org][1]

add_filter('woocommerce_available_payment_gateways', 'woocs_filter_gateways', 1);
 
function woocs_filter_gateways($gateway_list)
{
    global $WOOCS;
    $exclude = array(
        'paypal' => array('EUR', 'GBP'), //do not show paypal gate if current currency is EUR or GBP
        'stripe' => array('USD')//do not show stripe gate if current currency is USD
    );
    //***
    foreach ($exclude as $gateway_key => $currencies)
    {
        if (isset($gateway_list[$gateway_key]) AND in_array($WOOCS->current_currency, $currencies))
        {
            unset($gateway_list[$gateway_key]);
        }
    }
 
    return $gateway_list;
}