Getting a list of magento stores

Blazo picture Blazo · May 3, 2011 · Viewed 43.4k times · Source

How can I get a list of store groups under a website in Magento and then a list of stores from that store group?

Answer

Anton S picture Anton S · May 3, 2011

Try this to get the objects directly

Mage::app()->getWebsites(); < in file > app/code/core/Mage/Core/Model/App.php:920 
Mage::app()->getStores(); < in file > app/code/core/Mage/Core/Model/App.php:834

iterate over to get the needed scope of one specific website or store

foreach (Mage::app()->getWebsites() as $website) {
    foreach ($website->getGroups() as $group) {
        $stores = $group->getStores();
        foreach ($stores as $store) {
            //$store is a store object
        }
    }
}

For the future if you have similar questions here's how i discovered those answers within 60 seconds. First i grep for method names or similar method names with space before method name to see where the methods are defined

grep ' getStores' app/code -rsn 
grep ' getWebsites' app/code -rsn 

Second step is grep for usage samples to see how they are meant to use by core developers. For that i add >methodName to grep and this gives me list of files where this method is called and this will give us place to look for examples:

grep '>getWebsites' app/code -rsn