dorflv5944 2014-02-07 14:23
浏览 37
已采纳

使这个Magento嵌套循环更好

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条回答 默认 最新

  • douyingmou1389 2014-02-07 14:28
    关注

    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()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作