Magento-我如何通过GeoIP按国家运营商店



我想按客户的IP运行商店。

在Magento的后端,用户可以将concret Store配置为按国家/地区加载。

看了一眼,我看到了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);

我认为这对性能没有危险,因为这种方法只在之前没有的情况下创建对象

/**
* 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那么我会买这个或者做一个类似的扩展。

使用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获得国家/地区/商店的列表,而不是硬编码。

最新更新