Reading list Switch to dark mode

    List All Magento Stores in Magento2

    Updated 21 January 2017

     

    Magento manages multiple Websites, Stores as well as there store views. After installation by default one default Website, store and it’s store view created after that user can create multi Websites, Stores and there store views as per as requirement.

    This article demonstrate how to fetch list of all Magento stores in Magento 2.

     

    In earlier version(1.9.*) of Magento below codes helps to fetch list of all Magento stores,

    Searching for an experienced
    Magento Company ?
    Find out More
    $stores = Mage::getModel('core/store_api')->items();
    

     

    As there have several things are changed in Magento 2. So below code will help to fetch list of all stores in Magento 2.

    <?php
     
    namespace Webkul\Demo\Model;
    
    use \Magento\Store\Model\StoreRepository;
     
    class Stores extends \Magento\Framework\DataObject 
        implements \Magento\Framework\Option\ArrayInterface
    {
        /**
         * @var Rate
         */
        protected $_storeRepository;
         
        /**
         * @param StoreRepository      $storeRepository
         */
        public function __construct(
            StoreRepository $storeRepository
        ) {
            $this->_storeRepository = $storeRepository;
        }
      
        public function toOptionArray()
        {
            $stores = $this->_storeRepository->getList();
            $websiteIds = array();
            $storeList = array();
            foreach ($stores as $store) {
                $websiteId = $store["website_id"];
                $storeId = $store["store_id"];
                $storeName = $store["name"];
                $storeList[$storeId] = $storeName;
                array_push($websiteIds, $websiteId);
            }
            return $storeList;
        }
    }
    • _storeRepository: Is an object of “Magento\Store\Model\StoreRepository” class.
    • $store[“website_id”] : It will return website id.
    • $store[“store_id”] : It will return store id.
    • $store[“name”] : It will return store name.
    • $store[“group_id”] : It will return store group id.
    • $store[“code”] : It will return store code which is unique as per as store.
    • toOptionArray() : This  method will return list of stores.

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home