重要提示:我不想购买任何GeoIP扩展。我有一个具有多站点和多商店设置的Magento 2.1.9网站。我已经建立了KSA,阿联酋,中国,埃及等网站,每个网站下至少有2个商店视图,例如,对于KSA,我拥有阿拉伯语和英语商店视图。
我想根据用户所在国家/地区的IP地址向用户显示该网站。例如,对于从KSA访问的用户,ar_sa(阿拉伯语-沙特阿拉伯商店应为默认设置)类似于阿联酋的用户(ar_uae或en_uae)。
到目前为止,我已经完成了以下编码,并成功地从IP地址获得了国家/地区。
这是我的etc/frontend/events.xml文件:
<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>这是我的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();
    }
}但这只会更改商店名称,而不会更改其他内容,例如语言/货币或布局。
                  只需使用Webstack中免费提供的geoip(即php-geoip或geoip apache模块),然后将用户重定向到存储代码即可,即默认的magento MAGE_RUN_TYPE MAGE_RUN_CODE ....简单如1 2 3
                
                
                  
                    —
                    MagenX 
                    
                  
                
              
                  我可以通过自定义模块以某种方式使用MAGE_RUN_TYPE和MAGE_RUN_CODE吗?
                
                
                  
                    —
                    Abid Malik 
                    
                  
                
              
                  如果合适,您可以尝试以下扩展名:magedelight.com/magento-2-extensions/…   我希望它对您有用。
                
                
                  
                    —
                    Himmat Paliwal 
                    
                  
                
              
                  @AbidMalik您有任何解决方案吗?请在这里分享。我也需要相同的内容
                
                
                  
                    —
                    Ask Bytes 
                    
                  
                
              
                  @AskBytes-不,我还在四处徘徊。
                
                
                  
                    —
                    阿比德·马利克