2

I want to change store by location in magento. For that I create a popup of location dropdown when user open website in there browser, here user selects its location and website redirect to particular store.

public function getLocationInfoByIp($observer) {

        $location = $_POST['location'];
        switch ($location) {

            case "US": {
                  Mage::app()->setCurrentStore('en');
                  break;
            }
            case "IN": {
                Mage::app()->setCurrentStore('de');
                break;
            }
            default: {
                Mage::app()->setCurrentStore('en');
                break;
            }
       }
 }

When I submit popup after selecting location from dropdown, It change store. But after refresh it again change store to default.

So please suggest to resolve this issue.

2

1 Answer 1

2

Depending on whether you have the store code added to the URL's or not in the admin settings (system->configuration->web->url options->add store code to urls), or stores are under different domains, you can take 1 of 2 approaches.

With the store code setting enabled, or stores under different domains the store is determined purely by the URL, so to switch to a particular store you just need to redirect to the base URL (or some other page of your choosing) i.e.:

$store = Mage::getModel('core/store')->load($storeid);
$url = $store->getBaseUrl();
Mage::app()->getResponse()->setRedirect($url);

If you don't have the store code appearing in URL's and stores are under the same domain you just set a store cookie containing the code of the store you want to switch to:

$store = Mage::getModel('core/store')->load($storeid);
$url = $store->getBaseUrl();
Mage::app()->getCookie()->set(Mage_Core_Model_Store::COOKIE_NAME, $store->getCode(), true);
Mage::app()->getResponse()->setRedirect($url);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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