You can get all the stores id assigned to Specific website Using Magento 2.
Retrieve the Store website relation by the website id to fetch the number of store a specific website has available.
Useful Interface class:
Magento\Store\Api\StoreWebsiteRelationInterface
Let’s Consider Base(id 1) website with two stores available,
1. English Store. (id 3)
2. French store. (id 4)
<?php
namespace Jesadiya\GetStore\Model;
use Exception;
use Psr\Log\LoggerInterface;
use Magento\Store\Api\StoreWebsiteRelationInterface;
class StoreId
{
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var StoreWebsiteRelationInterface
*/
private $storeWebsiteRelation;
public function __construct(
LoggerInterface $logger,
StoreWebsiteRelationInterface $storeWebsiteRelation
) {
$this->logger = $logger;
$this->storeWebsiteRelation = $storeWebsiteRelation;
}
/**
* Get store ids from the website
*
* @return array
*/
public function getStoreIds()
{
$storeId = [];
$websiteId = 1; //website id
try {
$storeId = $this->storeWebsiteRelation->getStoreByWebsiteId($websiteId);
} catch (Exception $exception) {
$this->logger->error($exception->getMessage());
}
return $storeId;
}
}
Call from the class,
echo $getStoreIds = $this->getStoreIds();
Output: Array of store ids.
Array
(
[0] => 3
[1] => 4
)
Result will be all the store id to the website in system.
