2

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

2 Answers 2

3

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()
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that did work and it eliminated my loops! I suppose this 'collection' is connected to the Magento EAV model in a way? I'll probably have to read Alan Storm's articles about that.
The stores objects are not EAV. a collection is basically a list of objects.
1

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 =)

5 Comments

every loop can be a problem if used unnecessarily on a big dataset ;). But in this case it looks like it wouldn't be a problem, although there may be a more direct function by Mage
@cypherabe you are absolutely correct but without more context from OP we are all just guessing =)
Ok, I was thinking about nested foreach loops in general. As it turned out, @Marius suggested a better Magento function which eliminated the loops completely. But, are there some general rules that help to avoid nested loops? Thanks
If you are asking about something like "efficient PHP looping" then I am sure you will find plenty of advice through Google. Here a few I found: phpdreams.com/blog-posts/best-practice-array-loops.html stackoverflow.com/questions/12847502/… stackoverflow.com/questions/1742215/… stackoverflow.com/questions/3307409/… and make sure to look into something called pass by reference
To answer your question though, you can generally avoid nested PHP loops by properly querying your database with JOINs. I helped a guy perform a JOIN in this SO question if you are interested.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.