11

Important: I don't want to buy any GeoIP extension. I have a Magento 2.1.9 website with multi-site and multi-store setup. I have setup website for KSA, UAE, CHINA, EGYPT etc. and under each website are at least 2 Store views, e.g., for KSA I have Arabic and English store views.

I want to show the user the website according to his country as per IP Address. e.g., for users accessing from KSA the ar_sa (Arabic - Saudi arabia store should be default) similarly for users from UAE (ar_uae or en_uae).

I have done the following coding so far and got the country from IP address successfully.

This is my etc/frontend/events.xml file:

<config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='urn:magento:framework/Event/etc/events.xsd'>
    <event name='controller_action_predispatch'>
        <observer name='Asoft_GeoIP_Redirect' instance='Asoft\GeoIP\Observer\Redirect' />
    </event>
</config>

And this is my Observer/Redirect.php:

namespace Asoft\GeoIP\Observer;

class Redirect implements \Magento\Framework\Event\ObserverInterface
{

    protected $_objectManager;
    protected $_storeManager;
    protected $_curl;

    public function __construct(
        \Magento\Framework\ObjectManagerInterface $objectManager,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\HTTP\Client\Curl $curl
    ) {
        $this->_objectManager = $objectManager;
        $this->_storeManager = $storeManager;
        $this->_curl = $curl;

    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        //echo 'You are browsing from : '.$this->getCountryName();
        switch ($this->getCountryName()){
            case 'UAE':
                $store_id = '11';
                break;
            default :
                $store_id = '7';
        }$this->_storeManager->setCurrentStore($store_id);
    }

    public function getCountryName()
    {
        $visitorIp = $this->getVisitorIp();
        $url = "freegeoip.net/json/".$visitorIp;
        $this->_curl->get($url);
        $response = json_decode($this->_curl->getBody(), true);
        //echo '<pre>';
        //print_r($response);
        $countryCode = $response['country_code'];
        $countryName = $response['country_name'];
        $stateName = $response['region_name'];
        return $countryCode;
    }

    function getVisitorIp()
    {
        $remoteAddress = $this->_objectManager->create('Magento\Framework\HTTP\PhpEnvironment\RemoteAddress');
        return $remoteAddress->getRemoteAddress();
    }
}

But this changes only the store name and not other things - like language / currency or layout.

Ahmad
  • 652
  • 1
  • 6
  • 16
Abid Malik
  • 461
  • 1
  • 4
  • 20
  • simply use geoip freely provided in your webstack, either php-geoip or geoip apache module, then just redirect users to store code, as default magento MAGE_RUN_TYPE MAGE_RUN_CODE .... simple as 1 2 3 – MagenX Mar 04 '18 at 20:56
  • can i somehow use MAGE_RUN_TYPE and MAGE_RUN_CODE from custom module – Abid Malik Mar 06 '18 at 14:44
  • You can try below extension if it is suitable to you: https://www.magedelight.com/magento-2-extensions/geoip-advance-magento-2.html I hope it will be working fine for you. – Himmat Paliwal Nov 25 '18 at 13:16
  • @AbidMalik did you get any solutions.? Please share it here. I'm also need the same – Ask Xah Apr 09 '19 at 12:14
  • @AskBytes - no i am still wandering around. – Abid Malik Apr 09 '19 at 12:50
  • @AbidMalik did you try this https://github.com/ytorbyk/magento2-geo-store-switcher. ?? it works but it does not allow anyother storeview. it strictly stick with that storeview we set from admin panel. If possible can you try this module and share the experience here – Ask Xah Apr 09 '19 at 13:08
  • @AskBytes thank you for sharing. I will give it a try and share the feedback soon! – Abid Malik Apr 10 '19 at 15:10
  • @AbidMalik Try the Magefan language switcher extension That works great and fulfill all our requirements – Ask Xah Apr 11 '19 at 06:55
  • @AskBytes - Thank you for the suggestion. BTW I used magento2-geo-store-switcher. and it is in line with my requirements. Although, I might have to make some customizations in near future, otherwise it is a good basic module for my project. – Abid Malik Apr 12 '19 at 10:33
  • @AbidMalik Did you tried that module. Now in live on your project.? I also want to use that extension. If you make any customization for solving some issues on that module, don't forgot to update here. – Ask Xah Apr 13 '19 at 08:02
  • @AskBytes no i used it on staging site. takes us some time to work on production site. On staging site it is working almost as I need it to be. – Abid Malik Apr 13 '19 at 10:52

1 Answers1

0

I found by looking at the Magento default store switcher that it calls {{url}}?___store={{store_code}}. So you would have to redirect the user to the same url but adding the get parameter containing the store code , e.g https://www.my-store.com/sofas?__store=france

Note that this PHP detection of location and redirecting is never going to work if you are planing on using a caching technology like varnish which I think you should. If you do use varnish, then you can still use most of your code but it needs to be executed from an AJAX request after the page loads.