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')
{
    //__store=fr
}
if($mycountryis =='GB')
{
    //__store=uk
}
if($mycountryis =='DE')
{
    //__store=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(){
    $this->_storeManagerInterface->setCurrentStore(1);
}
  • 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.

Vendor\Module\etc\di.xml

<?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" />
</config>

Vendor\Module\Model\StoreResolver.php

/**
 * {@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());
$storeCookieManager->setStoreCookie($store);

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

 
discard

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.