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)?
This is corrected code