我正在尝试获取 Magento 商店上的所有商店。我所说的所有商店是指所有网站上的所有商店。我编写了这段代码并且它有效,但我有点担心嵌套 foreach 循环的复杂性。请看一下它,如果您认为我可以做一些不同的事情,请告诉我。
public function getAllStoresCustom(){
$all_stores = array();
foreach (Mage::app()->getWebsites() as $website) {
foreach ($website->getGroups() as $group) {
$all_stores [] = $group->getStores();
}
}
return $all_stores;
}
我只在 Magento 中找到了这些功能,所以我想我必须使用这些功能,而这似乎是唯一有效的组合。
非常感谢
最佳答案
试试这个:
$allStores = Mage::getModel('core/store')->getCollection();
然后在需要时循环遍历$allStores
foreach ($allStores as $store) {
//do something with $store
}
注意:您将获得一个 ID 为 0
的商店。这是管理商店 View 。如果您希望所有商店都没有管理商店 View ,请使用以下命令:
$allStores = Mage::getModel('core/store')->getCollection()->setWithoutDefaultFilter()
关于php - 使这个 Magento 嵌套循环更好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21630153/