3

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

core_block_abstract_prepare_layout_before

and in the Observer class I am doing this:

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

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

but this throws an exception:

Mage registry key "application_params" already exists

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.

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?

4

2 回答 2

6

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;
            }
       }
 }
于 2014-01-29T12:10:53.290 回答
0

U can redirect to the store view with the ip. No need to use Mage::run

  switch(trim($countryCode))  
                 {  
                       case 'CH':  
                                $url = $siteurl . '?___store=german';  
                                header( 'Location:' . $url);die;  
                       break;  
                       case 'DE':  
                                     $url = $siteurl . '?___store=german';  
                                     header( 'Location:' . $url);die;  
                       break;  
                       case 'IN':  
                                 $url = $siteurl . '?___store=english';  
                                 header( 'Location:' . $url);die;  
                       break;  
                       default:  
                       $url = $siteurl . '?___store=usa';  
                                 header( 'Location:' . $url);die;  
                       break;  
                 }  
于 2013-04-19T11:39:27.757 回答