Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a list of magento stores

Tags:

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?

like image 991
Blazo Avatar asked May 03 '11 09:05

Blazo


People also ask

How do I get all stores in Magento 2?

This article demonstrate how to fetch list of all Magento stores in Magento 2. $stores = Mage::getModel('core/store_api')->items(); $stores = Mage::getModel('core/store_api')->items(); $stores = Mage::getModel('core/store_api')->items();

How do I know if a site is Magento?

Check with https://builtwith.com/. In Ecommerce section it shows Magento if the site is built in Magento. If it is Magento 1 site, Ecommerce section shows Magento only.

How do I get Magento 2 store code?

You can get store ID by calling the block function in your phtml file. echo $block->getStoreId(); Using Object Manager.

How do I find the site ID in Magento 2?

You may need to get a current Store ID, Store Code, Name, Website ID, or current URL. You can use the object manager to quickly get the StoreManager object for a test, as in the example bellow: $objectManager = \Magento\Framework\App\ObjectManager::getInstance();


2 Answers

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 
like image 162
Anton S Avatar answered Sep 22 '22 14:09

Anton S


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.

like image 36
Eric Seastrand Avatar answered Sep 22 '22 14:09

Eric Seastrand