Is it possible to get a list of all sites programatically?
2 Answers
You can get all site of Store view using below code:
You can use below class for getting a list of store view and Magento store running from store view.
<?php
namespace StackExchange\Magento\Model;
class StoreList {
/**
* @var \Magento\Store\Api\StoreRepositoryInterface
*/
private $storeRepository;
public function __construct(
\Magento\Store\Api\StoreRepositoryInterface $storeRepository
) {
$this->storeRepository = $storeRepository;
}
public function getAllSiteList()
{
$storeviews = $this->storeRepository->getList();
foreach($storeviews as $store)
{
echo $store->getCode();
echo $store->getName();
echo $store->getStoreGroupId();
echo $store->getWebsiteId();
}
}
}
By sites do you mean websites ?
If yes you can use the storeManagerInterface :
protected $_storeManager;
public function __construct(\Magento\Store\Model\StoreManagerInterface $storeManager)
{
$this->_storeManager = $storeManager;
}
And in your function :
$this->_storeManager->getWebsites();
If you want to retrieve all names or other data you can use a while on it :
foreach ($this->storeManager->getWebsites() as $website) {
echo $website->getName();
}