Magento has the inbuilt facility to set up multistore functionality in the admin side.It can be managed from the front end by a store switcher or language switcher.But what if we needed to be automatically change store based on user location, Its simple though we need an extension for this to be done and some code to be run when the magento get loads with the help of the magento observers.
So first of all we need the extension named GeoIP which is free now and we can get that from Magento Connect Once the extension is installed activate them
Then we need to add the event observer in the xml file as shown below
<events>
<controller_action_predispatch> <!-- identifier of the event we want to catch -->
<observers>
<controller_action_predispatch_handler> <!-- identifier of the event handler -->
<type>model</type> <!-- class method call type; valid are model, object and singleton -->
<class>storeswitcher/observer</class> <!-- observers class alias -->
<method>getLocationInfoByIp</method> <!-- observer's method to be called -->
<args></args> <!-- additional arguments passed to observer -->
</controller_action_predispatch_handler>
</observers>
</controller_action_predispatch>
</events>
And in the observer we need to set the store based on the location
class Naviz_StoreSwitcher_Model_Observer
{
public function getLocationInfoByIp(Varien_Event_Observer $observer) {
$geoIP = Mage::getSingleton('geoip/country');
$cnCode = $geoIP->getCountry();
switch ($cnCode) {
case "US": {
Mage::app()->setCurrentStore('default');
break;
}
case "IN": {
Mage::app()->setCurrentStore('lca');
break;
}
case "CA": {
Mage::app()->setCurrentStore('lca');
break;
}
default: {
Mage::app()->setCurrentStore('default');
break;
}
}
}
}
Here lca,default are the two different store in which lca is for canada and default for all other countries
Download Source Code
