2

We are following this link to display store views based on country.

If someone visit site from USA, it will open us store view , Canada => Canada store view, so on.

We are displaying store view switcher on top as here. Someone from USA changed store view to Canada store view.

But once he click on any other page (menu, catalog, Product ,cms) it will again display old (USA) store view.

But it should be in same store view until we change it manually.

enter image description here

Observer.php :

class Atwix_Ipstoreswitcher_Model_Observer
{
    /**
     * redirects customer to store view based on GeoIP
     * @param $event
     */
    public function controllerActionPostdispatch($event)
    {

        if (!empty($_GET['___from_store'])) {
            Mage::app()->setCurrentStore($_GET['___store']);
        } else {
            $geoIP = Mage::getSingleton('geoip/country');
            $cnCode = $geoIP->getCountry();
            // echo $cnCode;
            // echo $cnCode='IN';
            switch ($cnCode) {
                case "US": {
                    Mage::app()->setCurrentStore('usa');
                    break;
                }
                case "IN": {
                    Mage::app()->setCurrentStore('india');
                    break;
                }
                case "CA": {
                    Mage::app()->setCurrentStore('canada');
                    break;
                }
                case "UK": {
                    Mage::app()->setCurrentStore('europe');
                    break;
                }

                case "AU": {
                    Mage::app()->setCurrentStore('australia');
                    break;
                }
            }

        }

    }
}
3

I did not test this solution yet, but I think it should work:

class Atwix_Ipstoreswitcher_Model_Observer
{
    /**
     * redirects customer to store view based on GeoIP
     * @param $event
     */
    public function controllerActionPostdispatch($event)
    {
        /** @var Mage_Customer_Model_Session $session */
        $session = Mage::getSingleton('customer/session');
        if (!empty($_GET['___from_store'])) {
            Mage::app()->setCurrentStore($_GET['___store']);
            $session->setData('current_country', $_GET['___store']);
        } else {
            $geoIP = Mage::getSingleton('geoip/country');
            $cnCode = $session->getData('current_country') ? $session->getData('current_country') : $geoIP->getCountry();
            // echo $cnCode;
            // echo $cnCode='IN';
            switch ($cnCode) {
                case "US": {
                    Mage::app()->setCurrentStore('usa');
                    break;
                }
                case "IN": {
                    Mage::app()->setCurrentStore('india');
                    break;
                }
                case "CA": {
                    Mage::app()->setCurrentStore('canada');
                    break;
                }
                case "UK": {
                    Mage::app()->setCurrentStore('europe');
                    break;
                }

                case "AU": {
                    Mage::app()->setCurrentStore('australia');
                    break;
                }
            }

        }

    }
}
  • No need to test your solution, your solution always worked for me. you are golden devoloper. thanks a lot. – Baby in Magento Jun 28 '16 at 9:12

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.