8

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

3
  • A little more context would help. The admin always runs with a store ID of 0, but I suspect that's not what you're asking here. "Current scoped store" is a little vague. Aug 16, 2013 at 21:02
  • Ya, definitely not looking for store id 0 =] Does my further description help describe what I am looking for? Aug 16, 2013 at 21:05
  • post code that is broken would give more clues
    – Anton S
    Aug 16, 2013 at 21:14

4 Answers 4

11

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.

6
  • Curious: why the strlen checks? Is it because the admin store is 0?
    – philwinkle
    Aug 16, 2013 at 21:51
  • Honestly it's a while back that I wrote the code so I'm not 100% sure but I can remember that it returns an empty string when it's not set (so when you're on a higher level)
    – Sander Mangel
    Aug 17, 2013 at 7:10
  • I was using this method successfully in Magento 1.8, but when tried in 1.6, it stopped working. Model is there and internally, it is using same methods, but on my code, it always return default level with 0 store id. Any idea you can provide why this is happening ?
    – Prateek
    Sep 14, 2015 at 7:46
  • @Prateek hm not sure. should indeed be working. Unfortunately I don't have Magento 1.6 on hand so hard to help you out there. You could do a compare between the files in app/code/core/Mage/Adminhtml and see what changed. Shouldn't be too long a list if you ignore comments (because of copyright lines)
    – Sander Mangel
    Sep 14, 2015 at 7:55
  • Store and Website both return blank for me in 1.9, Alan's answer below does work for me. May 8, 2017 at 11:34
7

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

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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.