How to Get All Stores’ List by Website ID in Magento 2
Are you a Magento 2 store admin handling a website with multiple stores in it?
If so, this solution to get all stores’ list by website ID in Magento 2 might be useful to you when you would want to implement different store-wise functionalities in your website. Likewise, in order to work with inventory among multiple warehouses easily, generate inventory-related reports, and manage stock effectively, one often needs to get stock id of particular website in Magento 2.
For example, you would want to show different currencies for each store based on the target audience.
Or, you might want to implement a different design.
Method to Get All Stores’ List by Website ID in Magento 2:
Create app/code/Vendore/Extension/Helper/Data.php
1 2 3 4 5 6 7 8 9 10 |
<?php namespace Vendore\Extension\Helper; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\App\Helper\Context; use Magento\Store\Model\StoreManagerInterface; class Data extends AbstractHelper { protected $storemanager; public function __construct( Context $context, StoreManagerInterface $storemanager ) { $this->storemanager = $storemanager; parent::__construct($context); } public function getStoresList($websiteID) { $stores = $this->storemanager->getWebsite($websiteID)->getStores();; return $stores; } } |
That’s it.
Any doubts? If so, do mention them in the Comments section below. I’d be happy to help.
Also, please share the solution with the Magento Community via social media.
Thank you.
Also Read: Magento 2 API – Get All Store IDs, Names, and Codes
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How to Integrate Facebook Shop with Magento 2 [2024]
How to Set and Get Admin Session Quote in Magento 2
Next