0

I am using Magento 2.1.7 and want to programatically load a specific store view on my multi-site based on what country a user is in when they arrive on our website. However in Magento 2 I am not sure how to do this. I tried doing the below. However it does not seem to work?? Any suggestions?

I have the below code

//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 =='DE')
  {
    //$this->_objectManager->get('Magento\Store\Model\StoreManagerInterface')->setCurrentStore(1);
  }
if($mycountryis =='GB')
  {
    //$this->_objectManager->get('Magento\Store\Model\StoreManagerInterface')->setCurrentStore(2);
  }
2

1 Answer 1

2

Add this code in index.php

$myipaddressis = $_SERVER['REMOTE_ADDR'];
$mycountryis = geoip_country_code_by_name($_SERVER['REMOTE_ADDR']);    
$store='your_default_store_code';
if($mycountryis =='DE')
  {
   $store='your de store code';
  }
$params = $_SERVER;
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = $store;
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'store';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
$app = $bootstrap->createApplication('Magento\Framework\App\Http');
$bootstrap->run($app);
1
  • how can we do this by a extension, By overriding which class help to achieve this
    – manu
    Commented Oct 6, 2022 at 17:43

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.