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.

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

Magento Fetch the store code by cookie using cookie value “store”

When you check the class, Magento\Store\Model\StoreCookieManager, one constant is defined as

const COOKIE_NAME = ‘store’;

StoreCookieManagerInterface mapped to StoreCookieManager Class and the function code is,

Output:

If Browser contains the cookie value equals to “store” it will store your current store code value.