How to Get Current Store ID from Current Scope in Magento Admin

adminadminhtmlmagento-1modulescope

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…

Global
-> 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…

Best Answer

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.

Related Topic