The 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.
The Solutions:
Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.
Method 1
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
Method 2
Mage::getModel('core/store')->load($storeCode, 'code')->getId()
Isn’t this the simplest way to do?
Method 3
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.
Method 4
The following should work:
<?php echo Mage::getModel('core/store')->loadConfig($code)->getId(); ?>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0