2012-11-13 85 views
3

我想通过客户的IP来运行存储。Magento - 我如何通过GeoIP按国家/地区运行商店?

在Magento的后端,用户可以配置混合存储以在每个国家加载。

以一眼,我看到该方法在类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; 
} 

在我的进步得到了很好的解决,我想另一种选择。

在index.php写下面的代码:

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

我认为这haven't危险的性能,因为这种方法只创建对象,如果你没有以前

/** 
    * 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; 
    } 

和我问题是......

这是获得解决方案的最佳方法吗?

,我认为是非常危险的修改甚至重写使用Mage_Core_Model_App

我不必须在层中的任何事件

另一种选择是由企业在index.php而是由后端

失去了管理

正在搜索......,找到了一个扩展程序,涵盖了我的许多要求, http://www.mageworx.com/store-and-currency-auto-switcher-magento-extension.html 然后我会购买这个或做了类似的扩展。

+0

我已经被分配了相同的任务,并试图寻找这样的事件,可以帮助我做到这一点,但我能够实现的唯一方法是(在通过访客ip获取[整个过程]国家后)重定向到特定(分配)的商店。 –

+0

这个模块可以帮助你http://www.mageworx.com/store-and-currency-auto-switcher-magento-extension.html – davidselo

回答

2

使用Magento或其他任何应用程序进行开发时,如果可以避免的话,切勿触摸任何核心文件。

这样做意味着未来可能的升级将覆盖您的更改并打破您的商店。

最简单的方法是做一切index.php,因为这是无论如何都选择商店的入口点,所有你正在做的是选择商店在不同的标准(即IP地址)。

一个简单的方法是使用免费的库,如maxmind GeoLite:http://dev.maxmind.com/geoip/geolite 您可以加载apache模块,也可以通过pecl扩展,甚至是普通的PHP。

这将返回您的访问者国家的ISO国家代码。

然后,您可以与商家代码的国家ISO代码命名您的商店,这将使它真正简单的依赖于IP

像这样简单的东西加载正确的店:

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

你当然可以将它制作成Magento扩展,但这是迄今为止最简单的方法。你甚至可以从Magento获得国家/商店的清单,而不是根据需要对它们进行硬编码。

相关问题