How to get current store id from current scope in admin

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...

Solutions

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

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.

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.

Similar questions

How to get store information (e.g Store Base Url) of another store in a multi-store setup for Magento 2
I have multiple stores on my Magento 2 installation. So I have store 1 base url as http://mystore1.com and store 2 base url as http://mystore2.com. How do i get the base url of store 2 dynamically on a phtml file in store 1. Note: $block->getBaseUrl() will only return the current store url. Is there a function like getBaseUrl of store id = 2 ?
Hide Current Store from link in store switch in multi store
I am facing one problem of multi-store option. Both stores are showing but I want to hide the current store and only show the other stores. These two options are showing at top left side. Store URL: www.image.pk Magento 1.9.1.0 version If we are in Export Leftovers than only polo republica show. if we are in polo republica than only export leftover...
Can I change the scope of product's "status" attribute to store level?
In magento backend you only have the choice between global and website scopes for the "status" attribute of a product. Can it be changed to the store level, so that different stores have different products activated? Will it have any side effects or is there another reason why it's not allowed to be on the store level by default?
Limit Maximum Qty Allowed in Shopping Cart for store/website scope
When I go to System -> Configuration -> Catalog -> Inventory -> Product Stock Options, Limit Maximum Qty appears under Default, but not when I change to a specific store scope. Is there a way to set this limit on a store/website level? Or do I have to set this option specifically on all products using Update Attributes in the Catalog Product admin ...
Website/Store View scope help
We are developing a website with the following requirements: Currently our set up is one website with multiple store views, one store view for UK and one for US. Using this module http://www.magentocommerce.com/magento-connect/store-view-pricing.html to set different fixed prices for each currency (ie. not based on conversion rates)... £0.99, $0.99...
how to change the system attribute scope from store to website
I am using magento 1.8. Note: I am using Advanced Permission to restrict user to a one store. I am developing multistore appication where I need to set product visibility = catalog, search for all the store. Means no need to select visibility in individual store scope. How can I set the product visibility scope global/website from store How can I c...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.