How to Retrieve Store code by cookie Magento 2?

You can get the current store code from the browser cookie using Magento.

Sometimes you required to detect store code before any request generated in a store and that time you will help this article. You can check the current store code in beforeDispatch() method of the request and verify for the current store code. Continue reading “How to Retrieve Store code by cookie Magento 2?”

Retrieve Store data by code programmatically Magento 2.

You can retrieve the store details by store code using Magento 2.

You required only store code to fetch data of the store. If store code is not valid throws the error,

The store that was requested wasn’t found. Verify the store and try again2.

Continue reading “Retrieve Store data by code programmatically Magento 2.”

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.