1

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)?

Michael
  • 41
  • 3
  • I am not sure, but i think if a _user currency_ exist it has to be set in `usermeta` table, and if it's the case, you could use the function `get_user_meta($user_id, $key, $single);` with also `$user_ID = get_current_user_id();`. But when searching and reading about similar cases over internet, i always fall on WPML multi-currency switcher not solved issues. The support team of WPML tell always that this kind of features are not supported and they kindly redirect people to expert developers. – LoicTheAztec Apr 12 '16 at 16:21

1 Answers1

0

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);
Michael
  • 41
  • 3