3
votes

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?

2
Did u get the solution for this? what are the steps u followed? I got struck at the same point from last 4 days. I too did created an event and called and the same switch case method and also the same error? please let me know.Pavan Kumar
unfortunately i had to redirect the page after setting the store, i left it there but kept in my revision list but i wasn't able to get back to it. hopefully soon will get around it and have it fixed.R T
for the time being this code is working for me but i don't think its a good approach.Mage::app()->getCookie()->set(Mage_Core_Model_Store::COOKIE_NAME, $storeCode, TRUE); Mage::app()->setCurrentStore($storeCode); $_baseurl = Mage::getBaseUrl(); echo "<script type='text/javascript'>window.location='{$_baseurl}'</script>";R T
Thank u for replying me back. I solved the issue and posted my solution below. Hope it may be useful for you also.Pavan Kumar

2 Answers

6
votes

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;
            }
       }
 }
0
votes

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;  
                 }