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
<?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.
Also, please share the solution with the Magento Community via social media.
Thank you.