gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:09:59 27 4
gpt4 key购买 nike

我想通过客户的 IP 经营商店。

在 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);

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

/**
* 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 获取国家/商店列表,而不是对它们进行硬编码。

关于Magento - 如何通过 GeoIP 按国家/地区经营商店?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13359258/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com