【发布时间】:2013-10-11 14:54:50
【问题描述】:
在 Magento 中,有几种方法可以加载一个或多个网站的所有商店。您可以执行Mage::app()->getStores(true) 或Mage::app()->getWebsites(),然后遍历结果集合中的所有商店。这个has already been answered here。我最近发现的是,在调用上述方法之一之前加载商店会影响结果。特别是关于默认商店。示例:
设置:1 个网站和 3 个商店(english、french、german,而 german 是默认商店)
Mage::app()->getStore()->load(0); // load admin store (or any other)
foreach (Mage::app()->getStores(true) as $store) {
echo "\n" . $store->getId() . " - " . $store->getCode();
}
result is:
0 - admin
1 - english
3 - french
Mage::app()->getStore()->load(2); // load german store (default)
foreach (Mage::app()->getStores(true) as $store) {
echo "\n" . $store->getId() . " - " . $store->getCode();
}
result is:
0 - admin
1 - english
3 - french
2 - german
当我浏览网站以获取它的商店时,甚至会发生更奇怪的事情。默认商店的值被当前加载的商店的值替换:
Mage::app()->getStore()->load(0); // load admin store
foreach (Mage::app()->getWebsites() as $website) {
foreach ($website->getStores() as $store) {
echo "\n".$store->getId() . ' - ' . $store->getCode();
}
}
result:
1 - english
3 - french
0 - admin
in case of Mage::app()->getStore()->load(1) the result is:
1 - english
3 - french
1 - english
我可以让所有商店独立于当前加载的商店的网站的唯一正确方法是这样的:
Mage::app()->getStore()->load($anyStoreId); // load any store
/** @var $websites Mage_Core_Model_Resource_Website_Collection */
$websites = Mage::getResourceModel('core/website_collection');
foreach ($websites as $website) {
foreach ($website->getStores() as $store) {
echo "\n".$store->getId() . ' - ' . $store->getCode();
}
}
result is always:
1 - english
3 - french
2 - german
这些结果的原因是什么?这是 Magento 中的错误还是这种行为是有意的?有没有更好的方法来加载网站的商店?
【问题讨论】: