6

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

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 – 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
  • @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. – David Coder Sep 19 at 6:40
0

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
0

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.

  • In which version of Magento 2 have you been able to do this? – Despotars Jun 4 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. – David Coder Sep 19 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 Sep 19 at 15:50

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.