在 Magento 中,有几种方法可以加载一个或多个网站的所有商店。您可以执行 aMage::app()->getStores(true)
或 aMage::app()->getWebsites()
然后遍历结果集合中的所有商店。这已经在这里得到了回答。我最近发现的是,在调用上述方法之一之前加载商店会影响结果。特别是关于默认商店。例子:
设置: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 中的错误还是这种行为是有意的?有没有更好的方法来加载网站的商店?