[英]Magento - How I can Run Store by Country by 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国家/地区代码。
然后,您可以使用商店代码的国家/地区代码命名您的商店,这将使得根据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获取国家/商店列表,而不是在需要时对其进行硬编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.