获取 magento 商店列表

发布于 2024-11-04 21:48:12 字数 45 浏览 0 评论 0原文

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

How can I get a list of store groups under a website in Magento and then a list of stores from that store group?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小猫一只 2024-11-11 21:48:12

尝试使用此方法直接迭代对象

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

Try this to get the objects directly

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

iterate over to get the needed scope of one specific website or store

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

For the future if you have similar questions here's how i discovered those answers within 60 seconds. First i grep for method names or similar method names with space before method name to see where the methods are defined

grep ' getStores' app/code -rsn 
grep ' getWebsites' app/code -rsn 

Second step is grep for usage samples to see how they are meant to use by core developers. For that i add >methodName to grep and this gives me list of files where this method is called and this will give us place to look for examples:

grep '>getWebsites' app/code -rsn
愛放△進行李 2024-11-11 21:48: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
                        )

                )

        )

)

为了发现这一点,我在管理员上找到了一个包含我想要的数据的多选,然后我打开根据模板提示找出哪个块类负责渲染它: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,我在其中找到了各种类似的也可能有用的方法。

Anton's answer, while correct, may be re-inventing the wheel just a bit. There is already a facility in the Magento Core to retrieve this sort of data.

You can retrieve a list of all websites, and their "children" using this:
Mage::getSingleton('adminhtml/system_store')->getStoresStructure()
You can also pass an array of websiteIds, storeIds, or storeGroupIds to the function, to filter the list:

public function getStoresStructure($isAll = false, $storeIds = array(), $groupIds = array(), $websiteIds = array())

Example output:

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
                                        )

                                )

                        )

                )

        )

)

There is a similar one used to populate the "Store Scope" dropdowns and multi-selects all across the admin section.

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
                        )

                )

        )

)

To discover this, I located a multi-select on the Admin that has the data I wanted, then I turned on template hints to find out which block class was responsible for rendering it: Mage_Adminhtml_Block_Cms_Page_Edit_Form. Knowing this, I found the class in the codebase,(app/code/core/Mage/Adminhtml/Block/Cms/Block/Edit/Form.php) and located the part that creates the input by searching for its label ("Store View"). This showed me how the input's values were being provided:

$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),
));

The Mage::getSingleton('adminhtml/system_store') points to the class Mage_Adminhtml_Model_System_Store, where I found a variety of similar methods that can also be useful. Have a look for yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文