Jump to content

Automatically change shop currency according to visitor's ip address/location


Recommended Posts

Hi

Is it at all possible that the site could automatically change the currency to accommodate the visitor, depending on the geographical location of his/her ip address?

I am currently using 3 currencies on the shop, USD, Euros & ZAR.
It would be great if the shop would automatically shift to Euros or ZAR (USD as default currency) if the visitor was coming from Europe or South Africa.


Any help/suggestion is appreciated.


Cheers

Link to comment
Share on other sites

tools.php

static public function setCurrency()

so in init.php somewhere after
tools::setCookieLanguage();


you write something like:

$hack_ArrCountryCurrency = ('de' => 'eur', 'us' => 'usd', ...);
$hack_countryCode = geoip_country_code_by_name($_SERVER['REMOTE_ADDR']);

if (array_key_exists($hack_countryCode, $hack_ArrCountryCurrency )) 
tools::setCurrency($hack_ArrCountryCurrency[$hack_countryCode]);



greetings

Link to comment
Share on other sites

Thanks for the reply, much appreciated.

Ok, I'm not too clued up with php's etc.., but I do edit a few files using FP on a regular basis.
So can I just basically paste the code into the tools.php file (anywhere in the file?), then just upload/replace existing one?

I will backup the original tools.php..


Thanks for your help.

Link to comment
Share on other sites

may work, or not ;)

i haven't tested this piece of code by myself.

i'd put it between line 22 and 23 (may vary if your ps version isn't cvs 6395).

it could also be that you have to put some files for the geoip thing on your server (i haven't used this thing too)

Link to comment
Share on other sites

  • 4 years later...

I did it for my site and it worked. Here is how I went about it. Prestashop cookies,variables are loaded through the front controller class found in the class folder when the site loads. So it is very easy to dictate the currency cookie at this point. You will need to write a function in Tools class and also Front controller.

 

So in front controller just after $currency = Tools::setCurrency(); edit that line and paste the following piece of code

$countrycode = $this->countryBasedOnIp();

global $currency;

if ($countrycode == 'KE') {

$currency = Tools::setCurrencyKenyan();

}

elseif ($countrycode != 'KE' ) {

$currency = Tools::setCurrency();

Then in front controller you will need to have the countryBasedOnIp() function which looks like this;

 

public static function countryBasedOnIp () {

 

global $currency;

 

/* Check if Maxmind Database exists (For more info on Maximind check their website) */

 

if (file_exists(_PS_GEOIP_DIR_.'GeoLiteCity.dat'))

{

include_once(_PS_GEOIP_DIR_.'geoipcity.inc');

include_once(_PS_GEOIP_DIR_.'geoipregionvars.php');

$gi = geoip_open(realpath(_PS_GEOIP_DIR_.'GeoLiteCity.dat'), GEOIP_STANDARD);

$record = geoip_record_by_addr($gi, Tools::getRemoteAddr());

 

}

 

return $record->country_code;

}

 

 

Then in tools.php you will create a function that return the exact currency cookie you need.In my case it was the Kenyan currency cookie.So the function looks like this.

 

public static function setCurrencyKenyan()

{

global $cookie;

 

$currency = Currency::getCurrencyInstance((int)(4));

if (is_object($currency) AND $currency->id)

$cookie->id_currency = (int)(4); //you have to assign the exact currency id

return $currency;

}

 

I hope this helps somebody out there

Link to comment
Share on other sites

  • 2 weeks later...

I tried geolocation by IP adress using the ciuntries sub-tab but it is not working for me. I need to get right becuase it might be holding the solution to a bug am facing with my earlier option. The default currency still shows up. I have changed the default currency for the shop under shipping >> countries >> then selected country Kenya and changed the default currency to KSHS. i have also enabled Geolocation. Anything am missing ?

Link to comment
Share on other sites

try this-->clean your cookies from your browser...as it remembers the last currency you selected..

 

this assumes you do not have your code (hack) messing things up...:)

 

also you need to set the country default currency...1.4 shipping-->countries-->edit the country-->select the default currency

Link to comment
Share on other sites

×
×
  • Create New...