Magento – magento set store view by store id

adminmagento-1.8multistorestore-view

Is it possible to set magento store view with help of store id and store code programmatically?

$store = Mage::getModel('core/store')->load($store_id); 
$code  = $store->getCode();

With the help of above code I can able to get store code. Now how to set store view with the help of id and code? I tried searching for solution but couldn't able to find it.

Best Answer

To implement this functionality I would have a look on how the select works.

The select calls a JS function switchStore which is defined here: app/design/adminhtml/default/default/template/dashboard/store/switcher.phtml:56

This function calls a url: \Mage_Adminhtml_Block_Store_Switcher::getSwitchUrl

and here I'm stuck. I have no idea where this url parameter is processed. So you have two options:

  1. reveal this secret or
  2. hook into admin_session_user_login_success after the login, redirect the user to the right store, which means get the url and set the store parameter.

It might be needed to die() in your controller which is really bad habit, this is because of the header() after the event:

app/code/core/Mage/Admin/Model/Session.php:104
Mage::dispatchEvent('admin_session_user_login_success', array('user' => $user));
header('Location: ' . $requestUri);