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,
$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.
Be the first to comment.