My code breaks WooCommerce Multilingual Currency Switcher

WPML Woocommerce Multilingual does not support currency setting depending on user location, so we made our own code:

function geoIPLocator() { 
global $woocommerce_wpml; 

$currency='EUR'; 
$geo=new WC_Geolocation(); 
$geo->init(); 
$country=$geo::geolocate_ip($geo::get_ip_address() ); 

if(isset($_SESSION['locator'])) { 
   if($_SESSION['locator']['IP']= =$_SERVER['REMOTE_ADDR'] && strlen($_SESSION['locator']['IP' ])>0) { 
    $woocommerce_wpml->multi_currency_support->s et_client_currency($_SESSION['locator'][ 'currency']); 

    return; 
   } 
} 

if($country['country']=="RU" || $country['country']=="BY") { 
    $woocommerce_wpml->multi_currency_support->s et_client_currency('RUB'); 
    $currency='RUB'; 
} else { 
    $woocommerce_wpml->multi_currency_support->s et_client_currency('EUR'); 
    $currency='EUR'; 
} 

$_SESSION['locator']=array("IP" =>$_SERVER['REMOTE_ADDR'], "ISO"=>$country['country'], "currency"=>$currency); 
} 

add_action( 'init', 'geoIPLocator', 5); 

The problem is that it breaks regular currency switcher. When this code is on, we can’t switch currency to any other. Is it because we don’t save or check if a user already has some currency set up to him (like the manual switcher does)?

Related posts

1 comment

  1. This is corrected code

    function geoIPLocator() {
       try {
               global $woocommerce;
    
               if ( isset($woocommerce) && gettype($woocommerce) == 'object' && property_exists($woocommerce,'session') && gettype($woocommerce->session) == 'object' && property_exists($woocommerce->session,'get') ) {
                       $manual_switch=$woocommerce->session->get('client_currency', null);
    
                       if ( $manual_switch == null ) {
    
                               global $woocommerce_wpml;
    
                               $currency='EUR';
                               $geo=new WC_Geolocation();
                               $geo->init();
                               $country=$geo::geolocate_ip($geo::get_ip_address());
    
    
                               if(isset($_SESSION['locator'])) {
                                       if($_SESSION['locator']['IP']==$_SERVER['REMOTE_ADDR'] && strlen($_SESSION['locator']['IP'])>0) {
                                               $woocommerce_wpml->multi_currency_support->set_client_currency($_SESSION['locator']['currency']);
    
                                               return;
                                       }
                               }
    
               if($country['country']=="RU" || $country['country']=="BY") {
                       $woocommerce_wpml->multi_currency_support->set_client_currency('RUB');
                       $currency='RUB';
               }  
               else {
                       $woocommerce_wpml->multi_currency_support->set_client_currency('EUR');
                       $currency='EUR';
               }
    
                               $_SESSION['locator']=array("IP"=>$_SERVER['REMOTE_ADDR'], "ISO"=>$country['country'], "currency"=>$currency);
                       }
               }
    
       } catch (Exception $e) {
               $e->getMessage();
       }
    
    }
    
    add_action( 'init', 'geoIPLocator', 5);
    

Comments are closed.