使用magento事件观察器以编程方式更改商店视图 [英] Change store view programmatically using magento event observer

查看:76
本文介绍了使用magento事件观察器以编程方式更改商店视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在捕捉以下事件以做进一步的逻辑:

I am catching the following event to do the further logic:

core_block_abstract_prepare_layout_before

在Observer类中,我正在这样做:

and in the Observer class I am doing this:

Mage::app()->setCurrentStore($storeView);

Mage::run($storeCode, "store");

但这会引发异常:

法师注册表项"application_params"已经存在

Mage registry key "application_params" already exists

基本上,我想做的是根据当前IP更改语言(商店视图)",我正在尝试通过magento自定义模块实现这一点.

basically what I am trying to do is "changing the Language (store view) according to the current IP" and I am trying to achieve this through magento custom module.

我希望能够使用magento事件观察器以编程方式更改magento的默认商店视图?并可以在不重定向到所选商店的情况下做到这一点,我的意思是通过在页面加载之前设置商店视图的值?

I want to be able to change the default store view of magento, programmatically using magento event observer? and would it be possible to do so without redirecting to the selected store I mean by setting the values for store view before page load?

推荐答案

这就是我解决问题的方式.我已经安装了GeoIP扩展程序.由此我们可以获取基于IP的国家/地区代码.我在观察器中添加了一个函数,该事件仅在页面加载事件controller_action_predispatch时触发一次,在观察器中,我使用开关盒在商店之间进行切换.这是我的功能&开关盒.

This is how I solved my problem. I have installed GeoIP extension. From this we are able to get the country code based on IP. I have added a function in the observer and this will be triggered only once on the page loads with the event controller_action_predispatch and in the observer, I use a switch case to switch between the stores. Here is my function & switch case.

public function getLocationInfoByIp($observer) {

        $geoIP = Mage::getSingleton('geoip/country');
        $cnCode =  $geoIP->getCountry(); 

        switch ($cnCode) {

            case "US": {
                  Mage::app()->setCurrentStore('en');
                  break;
            }
            case "IN": {
                Mage::app()->setCurrentStore('de');
                break;
            }
            default: {
                Mage::app()->setCurrentStore('en');
                break;
            }
       }
 }

这篇关于使用magento事件观察器以编程方式更改商店视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆