【发布时间】:2011-05-03 09:36:00
【问题描述】:
如何在 Magento 的网站下获取商店组列表,然后从该商店组中获取商店列表?
【问题讨论】:
标签: magento
如何在 Magento 的网站下获取商店组列表,然后从该商店组中获取商店列表?
【问题讨论】:
标签: magento
试试这个直接获取对象
Mage::app()->getWebsites(); < in file > app/code/core/Mage/Core/Model/App.php:920
Mage::app()->getStores(); < in file > app/code/core/Mage/Core/Model/App.php:834
迭代以获得特定网站或商店所需的范围
foreach (Mage::app()->getWebsites() as $website) {
foreach ($website->getGroups() as $group) {
$stores = $group->getStores();
foreach ($stores as $store) {
//$store is a store object
}
}
}
如果您以后有类似的问题,这里是我如何在 60 秒内找到这些答案的方法。首先,我 grep 查找方法名称或类似方法名称,方法名称前有空格,以查看方法的定义位置
grep ' getStores' app/code -rsn
grep ' getWebsites' app/code -rsn
第二步是 grep 使用示例,以了解核心开发人员如何使用它们。为此,我将 >methodName 添加到 grep,这给了我调用此方法的文件列表,这将为我们提供查找示例的地方:
grep '>getWebsites' app/code -rsn
【讨论】:
Anton 的回答虽然正确,但可能只是在重新发明轮子。 Magento Core 中已经有一个工具可以检索此类数据。
您可以使用以下方法检索所有网站及其“子项”的列表:
Mage::getSingleton('adminhtml/system_store')->getStoresStructure()
您还可以将 websiteIds、storeIds 或 storeGroupIds 的数组传递给函数,以过滤列表:
public function getStoresStructure($isAll = false, $storeIds = array(), $groupIds = array(), $websiteIds = array())
示例输出:
Array
(
[1] => Array
(
[value] => 1
[label] => Main Website
[children] => Array
(
[1] => Array
(
[value] => 1
[label] => Madison Island
[children] => Array
(
[1] => Array
(
[value] => 1
[label] => English
)
[2] => Array
(
[value] => 2
[label] => French
)
[3] => Array
(
[value] => 3
[label] => German
)
)
)
)
)
)
有一个类似的用于填充“存储范围”下拉列表和整个管理部分的多选。
Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm(false, true)
Array
(
[0] => Array
(
[label] => All Store Views
[value] => 0
)
[1] => Array
(
[label] => Main Website
[value] => Array
(
)
)
[2] => Array
(
[label] => Madison Island
[value] => Array
(
[0] => Array
(
[label] => English
[value] => 1
)
[1] => Array
(
[label] => French
[value] => 2
)
[2] => Array
(
[label] => German
[value] => 3
)
)
)
)
为了发现这一点,我在 Admin 上找到了一个包含我想要的数据的多选,然后我打开了模板提示以找出哪个块类负责呈现它:Mage_Adminhtml_Block_Cms_Page_Edit_Form。知道了这一点,我在代码库(app/code/core/Mage/Adminhtml/Block/Cms/Block/Edit/Form.php)中找到了该类,并通过搜索其标签(“Store看法”)。这向我展示了如何提供输入值:
$field =$fieldset->addField('store_id', 'multiselect', array(
'name' => 'stores[]',
'label' => Mage::helper('cms')->__('Store View'),
'title' => Mage::helper('cms')->__('Store View'),
'required' => true,
'values' => Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm(false, true),
));
Mage::getSingleton('adminhtml/system_store') 指向Mage_Adminhtml_Model_System_Store 类,我在其中发现了许多类似的方法,它们也很有用。 Have a look for yourself.
【讨论】: