Magento 2 multi site is setup. I want to know redirect programatically to a specific storeview based on country

So I have something like the below however it is not working because I believe the Mage::app code I have is for Magento 1.x and not Magento 2.x ??

 //lets redirect user to a country specific store based on where they are from
$myipaddressis = $_SERVER['REMOTE_ADDR'];
$mycountryis = geoip_country_code_by_name($_SERVER['REMOTE_ADDR']);

if($mycountryis =='FR')
if($mycountryis =='GB')
if($mycountryis =='DE')
if($mycountryis =='US')
  $_storeCode = Mage::app()->getStore(2)->getCode();
  $_storeName = Mage::app()->getStore(2)->getName();
  $_storeId = Mage::app()->getStore(2)->getId();

4 Answers 4


Use StoreManagerInterface to set current store. You can use observer to do so.

 * @var \Magento\Store\Model\StoreManagerInterface
protected $_storeManagerInterface;

//Inject your class
public function __construct(
    \Magento\Store\Model\StoreManagerInterface $storeManagerInterface
) {
    $this->_storeManagerInterface = $storeManagerInterface;

// Use It in your method
public function execute(){
  • 2
    i tried to it on admin login success event but its not working for me Commented May 16, 2018 at 8:02
  • I want to set store for particular admin user do you have any idea ? Commented May 6, 2019 at 5:47
  • @Priyank can you share the event name ? I want to set the current store when user open the site. once it's open then user can able to switch the store. But at first time I want to set the store view base on some condition. Commented Sep 19, 2019 at 6:40
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Controller\Result\RedirectFactory;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Api\StoreRepositoryInterface;
use Magento\Store\Model\StoreManagerInterface;

class Post implements HttpGetActionInterface
     * @var RedirectFactory
    private RedirectFactory $redirectFactory;

     * @var StoreManagerInterface
    private StoreManagerInterface $storeManager;

     * @var StoreRepositoryInterface
    private StoreRepositoryInterface $storeRepository;

     * Post constructor.
     * @param RedirectFactory $redirectFactory
     * @param StoreManagerInterface $storeManager
     * @param StoreRepositoryInterface $storeRepository
    public function __construct(
        RedirectFactory $redirectFactory,
        StoreManagerInterface $storeManager,
        StoreRepositoryInterface $storeRepository
    ) {
        $this->redirectFactory = $redirectFactory;
        $this->storeManager = $storeManager;
        $this->storeRepository = $storeRepository;

     * @return Redirect
    public function execute(): Redirect
        $redirect = $this->redirectFactory->create();

        try {
            $store = $this->storeRepository->get('your_store_code_name');
        } catch (NoSuchEntityException $e) {
            // Anything you need.


        return $redirect;

Your code will only work for M1. For M2 you would need to use your own StoreResolver model and overwrite getCurrentStoreId() function.


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Store\Model\StoreResolver" type="Vendor\Module\Model\StoreResolver" />


 * {@inheritdoc}
public function getCurrentStoreId()
    //Your logic here
  • ok but whats the logic I would put in the getCurrentStoreId() function?
    – Jayreis
    Commented May 30, 2017 at 21:37
  • You would need to put a logic around mapping country to your existing store. So, first you need to resolve the country based on IP, then decide which store to select based on the country.
    – Mageinn
    Commented May 31, 2017 at 8:29

The above solution did not work for me. What did work was doing something as follows:

$storeManager = $objectManager->create('\Magento\Store\Model\StoreManagerInterface');
$storeCookieManager = $objectManager->create('\Magento\Store\Api\StoreCookieManagerInterface');
$httpContext = $objectManager->create('\Magento\Framework\App\Http\Context');

$defaultStoreView = $storeManager->getDefaultStoreView();
$httpContext->setValue(\Magento\Store\Model\Store::ENTITY, $store->getCode(), $defaultStoreView->getCode());

Ideally initialize the classes in the class constructor and not using the object manager.

$store is the store to which you would like to programmatically redirect.

  • In which version of Magento 2 have you been able to do this?
    – Despotars
    Commented Jun 4, 2019 at 8:49
  • When we can run this code ? I want to set store view when user first open the site. Then he can select the store view. But at first time want to set the particular store base on some condition. Commented Sep 19, 2019 at 6:39
  • You can create an observer for the event controller_front_send_response_before. Alternatively, you can use the extension hungersoft.com/p/magento2-geoip-redirect. It has redirect once feature which does exactly what you want
    – hungersoft
    Commented Sep 19, 2019 at 15:50
  • what is the path for this code? where should i put this code? Commented Jan 12, 2021 at 11:16

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.