How to get the store id from the code
-
12-12-2019 - |
Question
So I have the store code and I would really like simply the store id. Ideally I do not want to simply load the store completely as I only need the id.
One idea I had was to load this via a collection but I was wondering if there was another way via a helper or resource.
Solution
Haven't tested this but I seem to remember you can get the ID by using the method loadConfig
in the model Mage_Core_Model_Store
After that you can get the ID by calling getId()
on the model.
The loadConfig
method uses the loaded configuration to retrieve store data
$store = Mage::getConfig()->getNode()->stores->{$code};
Which gives you the following values
- id
- code
- website_id
OTHER TIPS
Mage::getModel('core/store')->load($storeCode, 'code')->getId()
Isn't this the simplest way to do?
I don't know if this is something that's peculiar to 1.9.0.0 but I couldn't get either Mage::getConfig()->getNode()->stores->{$code}
or Mage::getModel('core/store')->loadConfig($code)
(which in fact calls Mage::getConfig()->getNode()->stores->{$code}
) to return anything.
The first was close... with a subtle change in form, this gets a result, though it's waaay more info than you need:
$store = Mage::getConfig()->getNode('stores')->{$code}
What worked for me, to get just the store id, was this:
$storeId = Mage::getConfig()->getNode('stores')->{$code}->system->store->id;
...still inefficient, of course, loading all that config info just for one little number. But hey, that's Magento for you.
The following should work:
<?php echo Mage::getModel('core/store')->loadConfig($code)->getId(); ?>