Getting a list of magento stores

后端 未结 2 781
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-03 23:09

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

2条回答
  •  情话喂你
    2021-02-03 23:53

    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.

提交回复
热议问题