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

Naviz_StoreSwitcher-v1

5 Comments on “Automatically change store based on user location magento

  1. setting up google authorship
    June 5, 2014

    If some one needs expert view about blogging and site-building then i suggest
    him/her to visit this weblog, Keep up the nice job.

  2. Paul Stanely
    April 29, 2015

    The code works fine but I am facing an issue and that is to allow the manual store switching.
    Admin please update this code , so that customer can manually switch to another store according to their requirements.

    Have a look at this extension, http://www.fmeextensions.com/magento-geo-ip-default-store-view.html

    • navaneeth
      December 19, 2015

      To make the default switching works you can use the above stated extension, but for automatic switching we ca use this code

  3. Akarsh Kumar
    April 28, 2016

    Will the above process work on magento 1.9.2.2 ?

    • navaneeth
      April 29, 2016

      Yes it will work

Leave a Reply

Your email address will not be published.