I want to change store view programmatically same as like when we switch store from store switcher at front end side.

I try this answer. But, it's not working. It's just change store view name from store switcher dropdown.

How to implement it?

Any help would be appriciated.


1 Answer 1


Try below solution to change store view programmatically

 * @var \Magento\Store\Api\StoreCookieManagerInterface $_storeCookieManager
protected $_storeCookieManager;

 * @var \Magento\Store\Api\StoreRepositoryInterface $_storeRepository
protected $_storeRepository;

public function __construct(
    \Magento\Store\Api\StoreCookieManagerInterface $storeCookieManager,
    \Magento\Store\Api\StoreRepositoryInterface $storeRepository
) {
    $this->_storeCookieManager = $storeCookieManager;
    $this->_storeRepository = $storeRepository;

public function execute(){
    $store = $this->_storeRepository->getActiveStoreByCode('store_view_code');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.