简体   繁体   English

Magento - 如何通过GeoIP按国家/地区运行商店?

[英]Magento - How I can Run Store by Country by GeoIP?

I want run store by IP of the customer. 我希望通过客户的IP运行商店。

In the backend of Magento, the user may configure the concret Store to load per country. 在Magento的后端,用户可以将concret Store配置为按国家/地区加载。

Taking a glance, I see the method at class Mage_Core_Model_App 一目了然,我在Mage_Core_Model_App类中看到了该方法

public function run($params)
{
    $options = isset($params['options']) ? $params['options'] : array();
    $this->baseInit($options);

    if ($this->_cache->processRequest()) {
        $this->getResponse()->sendResponse();
    } else {
        $this->_initModules();
        $this->loadAreaPart(Mage_Core_Model_App_Area::AREA_GLOBAL, Mage_Core_Model_App_Area::PART_EVENTS);

        if ($this->_config->isLocalConfigLoaded()) {
            //$scopeCode = isset($params['scope_code']) ? $params['scope_code'] : '';

            //===============custom scope by country======================

             $scopeCode = Mage::helper('custom/module')->getStoreByGeoip();

            //===============custom scope by country======================

            $scopeType = isset($params['scope_type']) ? $params['scope_type'] : 'store';
            $this->_initCurrentStore($scopeCode, $scopeType);
            $this->_initRequest();
            Mage_Core_Model_Resource_Setup::applyAllDataUpdates();
        }

        $this->getFrontController()->dispatch();
    }
    return $this;
}

In my progress to get a good solution, I thought another alternative. 在我获得良好解决方案的过程中,我想到了另一种选择。

In the index.php write the next code: 在index.php中编写下一个代码:

 Mage::app();
 Mage::Helper('custom/helper')->getRunCodeByGeoio();
 Mage::run($mageRunCode, $mageRunType);

I thinks this haven´t dangerous of performance because this method only create object if you not have before 我认为这对性能没有危害,因为这个方法只创建了对象,如果你以前没有

/**
     * Get initialized application object.
     *
     * @param string $code
     * @param string $type
     * @param string|array $options
     * @return Mage_Core_Model_App
     */
    public static function app($code = '', $type = 'store', $options = array())
    {
        if (null === self::$_app) {
            self::$_app = new Mage_Core_Model_App();
            self::setRoot();
            self::$_events = new Varien_Event_Collection();
            self::$_config = new Mage_Core_Model_Config();

            Varien_Profiler::start('self::app::init');
            self::$_app->init($code, $type, $options);
            Varien_Profiler::stop('self::app::init');
            self::$_app->loadAreaPart(Mage_Core_Model_App_Area::AREA_GLOBAL, Mage_Core_Model_App_Area::PART_EVENTS);
        }
        return self::$_app;
    }

And my question is...... 我的问题是......

Are this the best approach for get the solution?? 这是获得解决方案的最佳方法吗?

I think is very dangerous modify Mage_Core_Model_App even using rewrite 我认为即使使用重写也很难修改Mage_Core_Model_App

I don´t have any event at tier 我没有任何级别的活动

Another option is made the business in the index.php but lost the management by the backend 另一个选择是在index.php中创建业务,但后端丢失了管理

Searching...., found a extension that cover many of my requirements, http://www.mageworx.com/store-and-currency-auto-switcher-magento-extension.html then I'll buy this or made a similar extension. 搜索....,找到了一个涵盖我的许多要求的扩展, http://www.mageworx.com/store-and-currency-auto-switcher-magento-extension.html然后我会买这个或做了一个类似的扩展。

You shyould never touch any core files when developing with Magento, or any other application if you can avoid it. 在使用Magento或任何其他应用程序进行开发时,如果可以避免使用它,就不应该触摸任何核心文件。

Doing this will mean possible future upgrades will overwrite your changes and break your store. 这样做意味着未来可能的升级将覆盖您的更改并破坏您的商店。

The simplest way would be to do everything index.php as this is the entry point where the store is selected anyway, all you are doing is selecting the store on different criteria (ie IP address). 最简单的方法是做一切index.php,因为这是无论如何选择商店的入口点,你所做的就是选择不同标准的商店(即IP地址)。

One simple way would be to use a free library, such as maxmind GeoLite: http://dev.maxmind.com/geoip/geolite You can either load an apache module, or via a pecl extensions, or even plain PHP. 一种简单的方法是使用免费的库,例如maxmind GeoLite: http ://dev.maxmind.com/geoip/geolite你可以加载一个apache模块,或者通过一个pecl扩展,甚至是普通的PHP。

This will return you the iso country code for the country of your visitor. 这将返回访问者所在国家/地区的ISO国家/地区代码。

You could then name your stores with a country iso code for the store code, and this will make it really simple to load the correct store depending on IP 然后,您可以使用商店代码的国家/地区代码命名您的商店,这将使得根据IP加载正确的商店非常简单

something simple like this: 像这样简单的东西:

$countryCode = getUsersCountryCode(); // which ever method you use in here...

$stores = array(
    'gb',
    'us',
    'fr',
);

if(in_array(countryCode, $stores)) {
    Mage::run(countryCode, 'store');
}
else {
    Mage::run($mageRunCode, $mageRunType);
}

You could of course make it into a Magento extensions, but this is by far the simplest way. 你当然可以把它变成Magento扩展,但这是迄今为止最简单的方法。 You could even get a list of the countries/stores from Magento rather than hard coding them if you required. 您甚至可以从Magento获取国家/商店列表,而不是在需要时对其进行硬编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Magento - 如何到达商店国家? - Magento - How to get the store country? 如何删除magento结帐页面中国家/地区的下拉选项? 我只需要放一个国家 - How can I remove the dropdown option in country in checkout page in magento? I need to put only one country 我在哪里可以找到magento的国家号码? - Where can I find the country number in magento? 我如何以编程方式在magento中添加商店和商店视图 - How can I programmatically add a store and store view in magento Magento:设置国家magento的默认商店视图 - Magento: Set default store view for country magento 如何使用清漆页面缓存在magento上启用geoip - How to Enable geoip on magento with varnish page cache 编写Magento GeoIP商店重定向,共享链接出现问题 - Writing a Magento GeoIP store redirect, problems with sharing links 如何重定向一个 magento 网站,该网站在每个国家/地区的瑞典和巴基斯坦等域中有 2 个网站 - How can I redirect a magento website which having 2 website in a domain like Sweden and Pakistan for each country 如何将内联翻译导出到另一个Magento商店? - How can I export inline translations to another Magento store? 如何为不同的商店分别设置Magento商店时区 - How Can I Set Magento Store Timezone For Different Stores Separately
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM