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.

  • Depending of what you exactly want to achieve, there are better methods than editing the index.php which quite is part of the magento core. So, what is the goal? – Fabian Blechschmidt Jul 1 '14 at 7:48
  • Actually I have few users(vendors) who have created stores in magento admin panel. So, when they login into admin panel, I have to write an observer event(admin_session_user_login_success) to find which user has logged in so that I can set his store automatically. – Saravanan Sampathkumar Jul 1 '14 at 7:51
  • So what you want to do is set the select box in the upper left automatically? You are NOT talking about frontend stores? – Fabian Blechschmidt Jul 1 '14 at 7:53
  • Yes, exactly. I'm talking about backend. Not frontend. I want to set the select box automatically when user enters admin panel – Saravanan Sampathkumar Jul 1 '14 at 7:54

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:

Mage::dispatchEvent('admin_session_user_login_success', array('user' => $user));
header('Location: ' . $requestUri);
| improve this answer | |
  • Yes, even I have been looking into the core folder to see how this works and I was stuck too. As you said, passing the store parameter would solve my problem. Vendor user have access only to catalog > manage product link ( I have restricted access in users permission settings). Is it possible to get catalog > manage product link in my observer? If so how to do it? Can you guide? – Saravanan Sampathkumar Jul 1 '14 at 8:09

yes you can do it. for that edit index.php file, add the following

$store_id = 'your_store_id_here';
$mageRunCode = 'store view code';
$mageRunType = 'store';

Mage::run($mageRunCode, $mageRunType);
| improve this answer | |

For future refernce and anyone wondering how to implement such a thing. this is how I did something similar where I had to make every admin see only items (products, categories... etc) and store filters that are assigned to him/her.

First step: Modified the table admin_user adding a column to specify the website for that admin.

Step two Identifying where the changes need to be made, and apparently, there are multiple store switchers and each is used for a different page, categories has one, and products has another.

Step three Now I override/change the code in app/code/core/Mage/Adminhtml/Block/Store/Switcher.php in the function getWebsiteCollection to be something like this:

public function getWebsiteCollection()
    $collection = Mage::getModel('core/website')->getResourceCollection();
    $websiteIds = Mage::getSingleton('admin/session')->getUser()->getWebsiteIds();
    $storeIdInRequest    = (int) $this->getRequest()->getParam('store');
    if (!is_null($websiteIds)) {
            $website = explode(",",$websiteIds);
            $website = Mage::app()->getWebsite($website[0]);
                    $storIdYouWantToRedirectTo = // get The store Id you want to redirect to
                    $response = Mage::app()->getResponse();
    return $collection->load();

The previous code does two things:

  1. It Filters the Switcher's websites depending on the admin's websites
  2. If the current store is not specified in the request, it redirects to the same page but with a store parameter set to the needed value

Hope this helps... I'm still testing this, so I don't really know if this has any side effects, and I'm implementing on a more pages than you need, so if I find any tricks or gotcha's in this I will be updating this answer.

| improve this answer | |

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.