0

I'm trying this script to detect browser language and redirect to Italian store view if the language is Italian or English store view if the language is not Italian. The country code is correctly detected but I'm not able to redirect to the store view. I've also tried using header('Location: ' . $lang), lang was 'Italian' or 'default'. It works but I don't want to have in the URL the store view code.

Here is the script:

function getCountryCode(){
  $CountryCode = substr(getenv('HTTP_ACCEPT_LANGUAGE'), 0, 2);
  if ($CountryCode=='it') {
    $lang = 3;
  }
  else{ 
    $lang = 1;
 }

 return $lang;
}

$lang = getCountryCode();

$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();        

$objectManager->get('\Magento\Store\Model\StoreManagerInterface')-
>setCurrentStore(0);

2 Answers 2

0

To change store in magento 2 you need to change store code and website in index.php file

Here is some information about store view and website

Each website and each store view must have a unique identifier. This identifier is required to use MAGE_RUN_TYPE and MAGE_RUN_CODE as follows:

MAGE_RUN_TYPE can be either store or website

Use website to load a website in your storefront. Use store to load any store view in your storefront. MAGE_RUN_CODE is the unique website or store view code that corresponds to MAGE_RUN_TYPE

First you need to find out store code by current language using HTTP_ACCEPT_LANGUAGE or by using customer's IP

To change store you need to set store code while creating bootstrap object

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER); 


$params = $_SERVER;

$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = $store; // add store code here

$params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = $website;
2
  • So, if I'm trying to switch store views I should use 'store' for PARAM_RUN_TYPE ? Oct 28, 2017 at 14:22
  • No if you want to change stores you have to set store code in PARAM_RUN_CODE Oct 29, 2017 at 18:44
-1

Please comment below code in index.php

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('Magento\Framework\App\Http');
$bootstrap->run($app);

And then add below code

    $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);

    switch ($lang) {
        case "it":
            $params = $_SERVER; $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = '<YOUR_ITALIAN_STORE_CODE_HERE>'; 
            $params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website'; 
            $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params); 
            $app = $bootstrap->createApplication('Magento\Framework\App\Http'); 
            $bootstrap->run($app); 
            break;     
        default: 
            $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
            /** @var \Magento\Framework\App\Http $app */
            $app = $bootstrap->createApplication('Magento\Framework\App\Http');
            $bootstrap->run($app);
        break; 
    }

Make sure you took backup your original index.php file.

2
  • Thank you for the help, but I've tried it and it gives error: Exception #0 (Magento\Framework\Exception\NoSuchEntityException): No such entity Oct 28, 2017 at 13:57
  • I've found a solution, it is store and not website because i'm trying to switch store views Oct 28, 2017 at 16:10

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.