2012-11-13 82 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獲得國家/商店的清單,而不是根據需要對它們進行硬編碼。

相關問題