Hello Magento Friends,
Today we will learn to retrieve all stores of all websites in Magento 2.
Magento 2 provides the option to set up multiple websites and stores. When you need to retrieve the list of all stores, with default Magento you can get the stores for that particular website only. But if you have the requirement to get all the stores regardless of the website, this guide is for you.
Let me explain practically,
As per the above image, Indian-INR and Global-USD stores are available on different websites. So if you want to retrieve a list of all stores no matter what website they are in, you can use the below steps.
Steps to Get All Stores of All Websites in Magento 2:
Step 1: Go to the below file path
app\code\Vendor\Extension\etc\di.xml
Then add the code as follows
1 2 3 4 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Store\Block\Switcher" type="Vendor\Extension\Block\Switcher"/> </config> |
Step 2: Now move to the following file path
app\code\Vendor\Extension\Block\Switcher.php
After that add the following code snippet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
<?php namespace Vendor\Extension\Block; use Magento\Directory\Helper\Data; use Magento\Store\Model\Group; use Magento\Store\Model\Store; use Magento\Framework\App\ActionInterface; use Magento\Framework\App\ObjectManager; use Magento\Framework\Url\Helper\Data as UrlHelper; /** * Switcher block * * @api * @since 100.0.2 */ class Switcher extends \Magento\Store\Block\Switcher { /** * @var bool */ protected $_storeInUrl; /** * @var \Magento\Framework\Data\Helper\PostHelper */ protected $_postDataHelper; /** * @var UrlHelper */ private $urlHelper; /** * @param \Magento\Framework\View\Element\Template\Context $context * @param \Magento\Framework\Data\Helper\PostHelper $postDataHelper * @param array $data * @param UrlHelper $urlHelper */ public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\Data\Helper\PostHelper $postDataHelper, array $data = [], \Magento\Framework\Url\Helper\Data $urlHelper = null ) { $this->_postDataHelper = $postDataHelper; parent::__construct($context, $postDataHelper, $data); $this->urlHelper = $urlHelper ?: ObjectManager::getInstance()->get(\Magento\Framework\Url\Helper\Data::class); } /** * @return array */ public function getRawAllStores() { $websites = $this->_storeManager->getWebsites(); $stores = []; foreach ($websites as $website) { $websiteStores = $website->getStores(); foreach ($websiteStores as $store) { /* @var $store \Magento\Store\Model\Store */ if (!$store->isActive()) { // continue; } $localeCode = $this->_scopeConfig->getValue( Data::XML_PATH_DEFAULT_LOCALE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store ); $store->setLocaleCode($localeCode); $params = ['_query' => []]; if (!$this->isStoreInUrl()) { $params['_query']['___store'] = $store->getCode(); } $baseUrl = $store->getUrl('', $params); $store->setHomeUrl($baseUrl); $stores[$store->getGroupId()][$store->getId()] = $store; } } return $stores; } } |
Step 3: Finally, run the below commands
1 2 |
php bin/magento setup:di:compile php bin/magento cache:flush |
Conclusion:
Hence, with the help of this method, you can get all stores lists of all the websites of your Magento 2 store. If you face any difficulty, share with me through the comment and stay tuned with us for more Magento solutions.
Happy Coding!