How to get current store id from current scope in admin

The question:

How do I get the store id of the current scoped store in the admin?

For instance, if I use the drop down box in the upper left hand corner, and change the scope to a store view, how would I get the store id of the currently scoped store view…

-> Website
--> Store
---> Store View (This is currently the scope, how do I get this store's id?)

We are working on a admin module someone else wrote, it is broken, the fix for the module appears to be passing a helper the store id of the currently scoped store…

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

In, let’s say, a Model for options in a dropdown, you could retrieve the active level using the following piece of code:

if (strlen($code = Mage::getSingleton('adminhtml/config_data')->getStore())) // store level
    $store_id = Mage::getModel('core/store')->load($code)->getId();
elseif (strlen($code = Mage::getSingleton('adminhtml/config_data')->getWebsite())) // website level
    $website_id = Mage::getModel('core/website')->load($code)->getId();
    $store_id = Mage::app()->getWebsite($website_id)->getDefaultStore()->getId();
else // default level
    $store_id = 0;

This will always return an active store ID.

Method 2

It’s still a little unclear where, exactly, you’re trying to grab this information in the admin. I’ll assume it’s while processing a request for the the System -> Configuration section. Hopefully this will send you in the right direction

Like all form fields in Magento, you can grab the values of the fields from the request object. Doing something like this (from a controller action)

$current = Mage::app()->getRequest()->getParam('section');
$website = Mage::app()->getRequest()->getParam('website');
$store   = Mage::app()->getRequest()->getParam('store');

Would give you the field values for the section, website, and store elements passed by the switcher elements. You’re interested in the store element. Unfortunately, this form doesn’t pass the IDs of the store, but instead the store short code

<option value="store_code" url="..." selected="selected" style="">Store View Name</option>

This means you’ll need to take the short code in $store, and use it to fetch the store’s information from a core/store model collection.

$store   = Mage::app()->getRequest()->getParam('store');

    $c = Mage::getModel('core/store')->getCollection()
    ->addFieldToFilter('code', $store);        

    $item = $c->getFirstItem();


    var_dump('Store Value Not Set');

Method 3

Unfortunately there is no standard way to add scopes to admin pages, aside from the system configuration pages. If you only need it within the system configuration, the previous answers will help you.

If not, this is how it is done in other core admin pages:

Product management

On the product grid as well as on the product edit page, store is simply a URL parameter that contains the store id (there is no “website” scoping for products). It’s not processed centrally, instead the blocks that need it, access the parameter directly. For example this method in the product grid:

protected function _getStore()
    $storeId = (int) $this->getRequest()->getParam('store', 0);
    return Mage::app()->getStore($storeId);

Category management

It’s the same as in product management, just that you don’t see the URL parameter in the browser address bar because the forms are loaded with AJAX controller actions.

These are the only admin sections besides the system configuration that I know of, where you can switch scopes. If the custom admin module is an addition to the catalog management, use the store parameter like above. If it took those as an example, you might be able to use the request parameter in the same way. Take a look at the HTTP requests while using the module.

Method 4

This – I believe – is a better code. You may handle exceptions (try...catch), ignored in this version to avoid errors when store or website request parameter is not corresponding to an existing store or website.

protected function _getConfigScopeStoreId()
    $storeId = Mage_Core_Model_App::ADMIN_STORE_ID;
    $storeCode = (string)Mage::getSingleton('adminhtml/config_data')->getStore();
    $websiteCode = (string)Mage::getSingleton('adminhtml/config_data')->getWebsite();
    if ('' !== $storeCode) { // store level
        try {
            $storeId = Mage::getModel('core/store')->load( $storeCode )->getId();
        } catch (Exception $ex) {  }
    } elseif ('' !== $websiteCode) { // website level
        try {
            $storeId = Mage::getModel('core/website')->load( $websiteCode )->getDefaultStore()->getId();
        } catch (Exception $ex) {  }
    return $storeId;

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment