获取magento商店列表

Bla*_*azo 26 magento

如何获取Magento网站下的商店组列表,然后获取该商店组的商店列表?

Ant*_*n S 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
Run Code Online (Sandbox Code Playgroud)

迭代以获得一个特定网站或商店的所需范围

foreach (Mage::app()->getWebsites() as $website) {
    foreach ($website->getGroups() as $group) {
        $stores = $group->getStores();
        foreach ($stores as $store) {
            //$store is a store object
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

对于未来,如果你有类似的问题在这里我是如何在60秒内发现这些答案.首先,我在方法名称之前使用空格查找方法名称或类似的方法名称,以查看方法的定义位置

grep ' getStores' app/code -rsn 
grep ' getWebsites' app/code -rsn 
Run Code Online (Sandbox Code Playgroud)

第二步是grep for usage samples,看看它们是如何被核心开发人员使用的.为此,我将> methodName添加到grep,这给了我调用此方法的文件列表,这将为我们提供查找示例的位置:

grep '>getWebsites' app/code -rsn
Run Code Online (Sandbox Code Playgroud)


Eri*_*and 12

安东的回答虽然正确,但可能会重新发明轮子.Magento Core中已有一个工具可以检索此类数据.

您可以使用以下方法检索所有网站及其"子"的列表: Mage::getSingleton('adminhtml/system_store')->getStoresStructure() 您还可以将networkIds,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
                                        )

                                )

                        )

                )

        )

)
Run Code Online (Sandbox Code Playgroud)

有一个类似的用于填充"存储范围"下拉列表和多个选择全部管理部分.

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
                        )

                )

        )

)
Run Code Online (Sandbox Code Playgroud)

为了发现这一点,我在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),
));
Run Code Online (Sandbox Code Playgroud)

对于Mage::getSingleton('adminhtml/system_store')类的要点Mage_Adminhtml_Model_System_Store,在那里我发现了各种类似的方法,这些方法也很有用.看看你自己.