tushonline
tushonline

Reputation: 290

Hide payment methods in Woocommerce, according to selected currency

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

Upvotes: 2

Views: 3184

Answers (4)

Uwe
Uwe

Reputation: 85

Somewhat old thread, but still relevant in 2023, with Wordpress v6.2.2, WooCommerce v7.8.2 and PHP v8.2. I use WPMLs currency switcher, but that shouldn't matter, as the below code is only making calls to Vanilla WooCommerce. The code filters payment gateways by their supported currencies:

function foo_woocommerce_available_payment_gateways( $gateway_list ) {
    $supported_currencies_by_gateway = array(
        'mobilepay' => array('DKK'),  // only show MobilePay for Danish Crowns
        'vipps' => array('NOK'), // only show VIPPS for Norwegian Crowns
        'swish' => array('SEK') // only show SWISH for Swedish Crowns
    );
    foreach ($supported_currencies_by_gateway as $gateway_key => $currencies) {
        if ( isset( $gateway_list[ $gateway_key ] ) AND !in_array( get_woocommerce_currency(), $currencies, true ) ) {
            unset( $gateway_list[ $gateway_key ] );
        }
    }
    return $gateway_list;
}
add_filter( 'woocommerce_available_payment_gateways', 'foo_woocommerce_available_payment_gateways', 1 );

To hide certain gateways for unsupported currencies, remove the exclamation-mark from the if-clause AND !in_array( get_woocommerce_currency(), $currencies, true ), rename $supported_currencies_by_gateway to $unsupported_currencies_by_gateway and obviously define exclusion-currencies instead of inclusion-currencies like so:

function foo_woocommerce_available_payment_gateways( $gateway_list ) {
    $unsupported_currencies_by_gateway = array(
        'paypal' => array('EUR', 'GBP'), // hide paypal for EUR and GBP
        'stripe' => array('USD') // hide stripe for USD
    );
    foreach ($unsupported_currencies_by_gateway as $gateway_key => $currencies) {
        if ( isset( $gateway_list[ $gateway_key ] ) AND in_array( get_woocommerce_currency(), $currencies, true ) ) {
            unset( $gateway_list[ $gateway_key ] );
        }
    }
    return $gateway_list;
}
add_filter( 'woocommerce_available_payment_gateways', 'foo_woocommerce_available_payment_gateways', 1 );

Happy coding!

Upvotes: 0

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;
}

Upvotes: 1

tushonline
tushonline

Reputation: 290

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.

Upvotes: 0

Shrek
Shrek

Reputation: 347

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.

Upvotes: 1

Related Questions