如何获取Magento网站下的商店组列表,然后获取该商店组的商店列表?
答案 0 :(得分:82)
尝试这样直接获取对象
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 ' getStores' app/code -rsn
grep ' getWebsites' app/code -rsn
第二步是grep for usage samples,看看它们是如何被核心开发人员使用的。为此,我将&gt; methodName添加到grep,这给了我调用此方法的文件列表,这将为我们提供查找示例的位置:
grep '>getWebsites' app/code -rsn
答案 1 :(得分:12)
安东的回答虽然正确,但可能会重新发明轮子。 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.