1

we have 2 store views for 2 countries [India & USA ] with store view codes sv1 & sv2

what we want is ,

if customer visit the site from USA, it should open USA store-view

if customer visit the site from India, it should open Indian store-view

by checking IP address, it should work.

1

Well, to do that you will need a GeoIp module.

A good resource can be found here: https://www.atwix.com/magento/geoip-magento-store-switcher/

First you'll have to install the GitHub module: https://github.com/tim-bezhashvyly/Sandfox_GeoIP

Then you will have to synchronize the IP database under System Configuration->General->Countries Options

From that point you will need to create your own module with the following helper:

/* app/code/local/Atwix/Ipstoreswitcher/Helper/Data.php */
class Atwix_Ipstoreswitcher_Helper_Data extends Mage_Core_Helper_Abstract
{
    const DEFAULT_STORE = 'English';

    /**
     * countries to store relation
     * default is English
     * @var array
     */
    protected $_countryToStore = array(
        'US' => 'sv2',
        'IN' => 'sv1'
    );

    /**
     * get store view name by country
     * @param $country
     * @return bool
     */
    public function getStoreByCountry($country)
    {
        if (isset($this->_countryToStore[$country])) {
            return $this->_countryToStore[$country];
        }
        return self::DEFAULT_STORE;
    }
}

Then you will need to observer the controller_action_postdipatch event to redirect your customers:

<?xml version="1.0"?>
<!--app/code/local/Atwix/Ipstoreswitcher/etc/config.xml-->       
<config>
 ...
    <global>
    ...
    </global>
    <frontend>
        <events>
            <controller_action_postdispatch>
                <observers>
                    <atwix_ipstoreswitcher>
                        <class>atwix_ipstoreswitcher/observer</class>
                        <method>controllerActionPostdispatch</method>
                    </atwix_ipstoreswitcher>
                </observers>
            </controller_action_postdispatch>
        </events>
    </frontend>
</config>

Finally your observer:

/* app/code/local/Atwix/Ipstoreswitcher/Model/Observer.php */

class Atwix_Ipstoreswitcher_Model_Observer
{
    /**
     * redirects customer to store view based on GeoIP
     * @param $event
     */
    public function controllerActionPostdispatch($event)
    {
        $cookie = Mage::getSingleton('core/cookie');
        if ($cookie->get('geoip_processed') != 1) {
            $geoIPCountry = Mage::getSingleton('geoip/country');
            $countryCode = $geoIPCountry->getCountry();
            if ($countryCode) {
                $storeName = Mage::helper('atwix_ipstoreswitcher')->getStoreByCountry($countryCode);
                if ($storeName) {
                    $store = Mage::getModel('core/store')->load($storeName, 'code');
                    if ($store->getName() != Mage::app()->getStore()->getName()) {
                        $event->getControllerAction()->getResponse()->setRedirect($store->getCurrentUrl(false));
                    }
                }
            }
            $cookie->set('geoip_processed', '1', time() + 86400, '/');
        }
    }
}
  • First of all thanks for nice answer , i tried a lot from past hours. seems i am missing some where. its not working for me – Baby in Magento Apr 29 '16 at 12:24
  • i set up like this : prnt.sc/aya5bh & code : 'US' => 'sv9', 'IN' => 'sv9', 'Fr' => 'sv9' in Data.php – Baby in Magento Apr 29 '16 at 12:24
  • i am in india, whenever i opened site, it should display currency - "Japanese yen" , but its not displaying. – Baby in Magento Apr 29 '16 at 12:25
  • @BabyInMagento Try adding some Mage::log() in the observer to check the following variables $countryCode, & $storeName – Raphael at Digital Pianism Apr 29 '16 at 12:26
  • pastebin.com/sWfGqiNW please chekc this , i added code at end of the file – Baby in Magento Apr 29 '16 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.