How to get current store id from current scope in admin

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.


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');

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

    $item = $c->getFirstItem();

    var_dump($item->getData());

    var_dump($item->getStoreId());
}
else
{
    var_dump('Store Value Not Set');
}

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.