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();

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 – Sarvesh Tiwari May 16 '18 at 8:02
  • I want to set store for particular admin user do you have any idea ? – Mukesh Prajapati May 6 at 5:47

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 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.

  • In which version of Magento 2 have you been able to do this? – Despotars Jun 4 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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