Retrieve list of all stores programmatically in Magento 2.

Get list of all the stores in a system Programmatically Magento 2.

Store contains the information regarding, store_id, code, website_id, group_id, name, sort_order and is_active.

You can see all the list from the database table name, store

Interface Provides the functionality:
Magento\Store\Api\StoreRepositoryInterface

<?php
namespace Jesadiya\AllStore\Model;

use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Api\StoreRepositoryInterface;

class StoreList
{
    /**
     * @var StoreRepositoryInterface
     */
    private $storeRepository;

    public function __construct(
        StoreRepositoryInterface $storeRepository
    ) {
        $this->storeRepository = $storeRepository;
    }

    /**
     * Get Store list
     *
     * @return StoreInterface[]
     */
    public function getAllStoreList(): array
    {
        $storeList = $this->storeRepository->getList();

        return $storeList;
    }
}

Fetch all the store using iterate over a loop from the method,

$stores = $this->getAllStoreList();
foreach ($stores as $store) {
    echo $store->getStoreId(); // store id
    echo $store->getCode(); // store code
    echo $store->getName(); // name
    echo $store->getIsActive(); // is active?
    echo $store->getGroupId(); // group id
    echo $store->getSortOrder(); // Sort Order
}

Output: Array of all the store list in the system.

Retrieve total Website count from the System Magento 2.

You can use getCount() method to retrieve total number of website available in the system using Magento 2.

You can manually count all the website list from the Admin panel via Stores -> Setting -> All stores. Continue reading “Retrieve total Website count from the System Magento 2.”

How to get all store ids assigned to specific website Magento 2?

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 Continue reading “How to get all store ids assigned to specific website Magento 2?”