Pergunta

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;
                }
            }

        }

    }
}
Foi útil?

Solução

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;
                }
            }

        }

    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange
scroll top