Skip to content

Instantly share code, notes, and snippets.

@miguelbalparda
Last active January 2, 2016 00:09
Show Gist options
  • Save miguelbalparda/8221903 to your computer and use it in GitHub Desktop.
Save miguelbalparda/8221903 to your computer and use it in GitHub Desktop.
Change store by location - Magento
$ip = $_SERVER["REMOTE_ADDR"];//cant use helper, not loaded yet
$location = file_get_contents('http://api.ipinfodb.com/v3/ip-city/?key=<api_key>&format=json&ip='.$ip.'');
$location = json_decode($location);
$country = $location->countryCode;
if(strcmp($country,'CA') == 0) {
$mageRunType = 'store';
$mageRunCode = 'en_ca';
}
else
{
$mageRunType = 'store';
$mageRunCode = 'default';
}
Mage::reset();
@miguelbalparda
Copy link
Author

Add this to index.php just before Mage::run($mageRunCode, $mageRunType);

@MagePsycho
Copy link

Why not to use maxmind geoip.dat + geoip.inc? At least there is no overhead of third party request.

@miguelbalparda
Copy link
Author

Agree MagePsycho, this can be combined with geoip but Magento is not loaded at this point, so it should be a plain PHP script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment