Magento - Redirect on Login via Observer

风流意气都作罢 提交于 2019-12-08 08:56:39

I know this post is a bit old but I see have it worked out for a module of my own.

I run the redirect on 2 different events. When the customer registers or when a customer logs in.

  <customer_register_success>
    <observers>
      <redirectAfterRegister>
        <class>businessdirectory/profile_observer</class>
        <method>redirectAfterRegister</method>
      </redirectAfterRegister>
    </observers>      
  </customer_register_success>
  <customer_login>
    <observers>
      <redirectAfterLogin>
        <class>businessdirectory/profile_observer</class>
        <method>redirectAfterLogin</method>
      </redirectAfterLogin>
    </observers>      
  </customer_login>     

I edited this answer because after testing I realized that two separate observers are needed for the two events. The good news is that you do not need to override files or rewrite controllers.

For the customer_register_success event, all you need to do is set the success_url param to whatever you want the url to be.

For the customer_login event, you will notice in your controller customer/account/_loginPostRedirect that the redirect URLs are being set on the customer session. As you can see in the second observer method below, all you need to do is grab the customer session and set the URL. For my purposes, I set the BeforeAuthUrl and AfterAuthUrl.

  public function redirectAfterRegister($observer)
  {     
    if(Mage::app()->getRequest()->getParam('listing_id') || Mage::app()->getRequest()->getParam('directory_id')){   
      $_session = Mage::getSingleton('customer/session');
      $action   = $_session->getDirectoryAction();

      if(Mage::app()->getRequest()->getParam('listing_id')){
        $listingId = Mage::app()->getRequest()->getParam('listing_id');
        Mage::app()->getRequest()->setParam('success_url',Mage::getUrl($action,array('listing_id'=>$listingId)));       
      }elseif(Mage::app()->getRequest()->getParam('directory_id')){
        $directoryId = Mage::app()->getRequest()->getParam('directory_id');
        Mage::app()->getRequest()->setParam('success_url',Mage::getUrl($action,array('directory_id'=>$directoryId)));       
      }
    }
  } 

  public function redirectAfterLogin($observer)
  {     
    if(Mage::app()->getRequest()->getParam('listing_id') || Mage::app()->getRequest()->getParam('directory_id')){   
      $_session = Mage::getSingleton('customer/session');
      $action   = $_session->getDirectoryAction();

      if(Mage::app()->getRequest()->getParam('listing_id')){
        $listingId = Mage::app()->getRequest()->getParam('listing_id');
        $url = Mage::getUrl($action,array('listing_id'=>$listingId));   
        $_session->setBeforeAuthUrl($url);
        $_session->setAfterAuthUrl($url);
      }elseif(Mage::app()->getRequest()->getParam('directory_id')){
        $directoryId = Mage::app()->getRequest()->getParam('directory_id');
        $url = Mage::getUrl($action,array('directory_id'=>$directoryId));
        $_session->setBeforeAuthUrl($url);
        $_session->setAfterAuthUrl($url);           
      }
    }
  } 

When you have a problem instantiating an object with $observer->getRequest(), try using $observer->getEvent()->getFront()->getResponse() instead. I believe this differs depending on what event the observer is listening to.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!