0

In order to automatically switch store views depending on a user's location we have decided to add the code provided here:Magento 2 pragmatically set storeview to ourindex.php file as shown below:

<?php
/**
 * Application entry point
 *
 * Example - run a particular store or website:
 * --------------------------------------------
 * require __DIR__ . '/app/bootstrap.php';
 * $params = $_SERVER;
 * $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'website2';
 * $params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website';
 * $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
 * \/** @var \Magento\Framework\App\Http $app *\/
 * $app = $bootstrap->createApplication(\Magento\Framework\App\Http::class);
 * $bootstrap->run($app);
 * --------------------------------------------
 *
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

try {
    require __DIR__ . '/app/bootstrap.php';
} catch (\Exception $e) {
    echo <<<HTML
<div style="font:12px/1.35em arial, helvetica, sans-serif;">
    <div style="margin:0 0 25px 0; border-bottom:1px solid #ccc;">
        <h3 style="margin:0;font-size:1.7em;font-weight:normal;text-transform:none;text-align:left;color:#2f2f2f;">
        Autoload error</h3>
    </div>
    <p>{$e->getMessage()}</p>
</div>
HTML;
    exit(1);
}

$myipaddressis = $_SERVER['REMOTE_ADDR'];
$mycountryis = geoip_country_code_by_name($_SERVER['REMOTE_ADDR']);
$store='default';
if($mycountryis =='FR')
  {
   $store='FR';
  }
if($mycountryis =='ES')
  {
   $store='ES';
  }

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

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

$app = $bootstrap->createApplication(\Magento\Framework\App\Http::class);
$bootstrap->run($app);

Although we did face some installation hurdles getting GeoIP work, we know that we have correctly installed the geoip_country_code_by_name function as we have tested it with a simple php file using the code below:

<?php
$myipaddressis = $_SERVER['REMOTE_ADDR'];
$mycountryis = geoip_country_code_by_name($_SERVER['REMOTE_ADDR']);
$country = geoip_country_code_by_name('www.google.com');
if ($country) {
    echo 'This host is located in: ' . $country;
    echo ' ---- My ip address is: ' . $myipaddressis;
    echo ' ---- My country is: ' . $mycountryis;
}
?>

which produces the following output:

www.google.com is located in: US ---- My ip address is: 92.150.xxx.xx ---- My country is: FR

Therefore we know the issue is not comming from GeoIP. However, when we navigate to our store homepage nothing happens in other words the location does not change but we do not get any errors either - just the default page. We have checked error/debug logs and see nothing there which would hint at where the issue is coming from. Our guess is that there is something wrong in the code we have added to theindex.php file. Any pointers would be greatly appreciated.

0

As usual the devil is in the detail... onthe server side index.php we were using

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

instead of

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

so in the example in the original question above actually works but we hadn't defined the $boostrap variable correctly when adding the code so the instructions weren't being passed on... hope the question is helpful for anyone wanting to use GeoIP.

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.