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();
up vote 1 down vote accepted

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(){
  • 1
    i tried to it on admin login success event but its not working for me – Er Sarvesh V Tiwari May 16 at 8:02

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="" 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 May 30 '17 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 May 31 '17 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.

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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