Elxis CMS Forum

Extensions => Modules => Topic started by: wieyoga on January 13, 2016, 02:54:45

Title: IOSR - local currency
Post by: wieyoga on January 13, 2016, 02:54:45
Please advise how to display local currency based on visitor location  :)
Title: Re: IOSR - local currency
Post by: datahell on January 13, 2016, 09:32:29
You can't detect visitor's country reliable. And even if you do so you don't know if he prefers to switch currency. For example I connect to the internet via a proxy server located in Germany but I live in Greece. Only if you use an "IP to country" API, or an IP to country database like this (http://www.ip2location.com/databases/db1-ip-country), provided by a third party then the detection could be good enough. From the moment you detect the correct country then you must have a table to assign the proper currency. When you find the currency you can switch currency like that:

$currency = 'EUR';
$eSession = eFactory::getSession();
$eSession->set('rescurrency', $currency);

This detection should not happen on each user click but only on his first visit on site. So you need an additional session check. After you switch the user you must refresh the page to see the new currency. Such code can be placed in your template's index.php file.

Full example
$eSession = eFactory::getSession();
$ok = $eSession->get('resautocurrency', 0);
if (!$ok) {
      $country = something_to_detect_user_country();
      if (($country == 'GR') ||  ($country == 'ES') ||  ($country == 'FR') ||  ($country == 'DE') ||  ($country == 'IT')) {
            $currency = 'EUR';
      } else if ($country == 'RU') {
           $currency = 'RUB';
      } else if ($country == 'GB') {
           $currency = 'GBP';
      } else if ($country == 'US') {
           $currency = 'USD';
      } else {
           $currency = 'EUR';
      }
      $eSession->set('rescurrency', $currency);
      $eSession->set('resautocurrency', 1);
      $url = eFactory::getURI()->getRealUriString();
      eFactory::getElxis()->redirect($url);
}