I am trying to get all stores on a Magento shop. By all stores, I mean all stores from all websites. I wrote this code and it works, but I'm a little concerned about the complexity of the nested foreach loop. Please take a look at it and advise me if you think I can do something different.
public function getAllStoresCustom(){
$all_stores = array();
foreach (Mage::app()->getWebsites() as $website) {
foreach ($website->getGroups() as $group) {
$all_stores [] = $group->getStores();
}
}
return $all_stores;
}
I've only found these functions in Magento, so I think I had to use those and this seemed the only combination that worked.
Thanks a lot
Try this:
$allStores = Mage::getModel('core/store')->getCollection();
Then loop through $allStores
when needed
foreach ($allStores as $store) {
//do something with $store
}
Notice: You will get a store with id 0
. That is the admin store view. If you want all stores without the admin store view use this:
$allStores = Mage::getModel('core/store')->getCollection()->setWithoutDefaultFilter()
foreach()
is an incredibly efficient PHP function so you can bet that it is not going to be your slowdown. If you are looking to optimize something then look into the code for the getStores()
and getGroups()
functions because those are being called within the iterations whereas getWebsites()
gets called only once.
If you want more guidance then please feel free to update your question with the contents of those functions.
You may also want to try https://codereview.stackexchange.com/ for more experienced opinions especially since you don't have any specific programming issue/error =)