Magento - 通过观察者登录时重定向 [英] Magento - Redirect on Login via Observer

查看:67
本文介绍了Magento - 通过观察者登录时重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在登录后将 Magento 中的特定用户重定向到 products.html 页面.由于重定向过多,我尝试执行此操作的大多数方式(setRedirect、header)都会导致循环.

I am trying to redirect specific users in Magento to the products.html page after logging in. Most every way I attempt to do this (setRedirect, header) results in a loop due to too many redirects.

目前,我正在尝试使用观察者来 setParam('return_url', "$customerRedirectUrl") 如这篇相当受欢迎的帖子所述 - Magento - 从观察者方法重定向客户.变量 $customerRedirectUrl 代表的逻辑共同可靠地创建了我知道我想要的页面的 url.我的相关代码如下所示.

Currently, I am trying to use an observer to setParam('return_url', "$customerRedirectUrl") as described in this rather popular post - Magento - Redirect Customer from Observer Method. The variable $customerRedirectUrl represents logic that together reliably creates the url I know I want the page to be. My relevant code looks like this.

   public function redirect(Varien_Event_Observer $observer)
{
    $helper = Mage::helper('personal_order');
    $isPersonalOrderStore = $helper->isPersonalOrderStore(null);
    $session = Mage::getSingleton('customer/session');



    if ($isPersonalorderStore){
        if(!$session->isLoggedIn()) {
            $targetUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB).'customer/account/login';
            Mage::app()->getResponse()->setRedirect($targetUrl);


      } else {
           $baseUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
           $storeName =  strtolower(Mage::app()->getStore()->getName());
           $storeName = str_replace(' ', '', $storeName);
           $customerRedirectUrl = "$baseUrl$storeName/products.html";

           $observer->getRequest()->setParam('return_url',"$customerRedirectUrl");
        }
    }

现在对我来说最重要的部分是 else 语句,该函数的其余部分工作正常.但是,调用 setParam 会导致以下错误:

The important part for my purposes now is the else statement, the rest of the function works fine. However, the call to setParam results in the following error:

致命错误:在非对象上调用成员函数 setParam()...

Magento - 从观察者方法重定向客户 帖子怎么样?没有得到(由于是一个相当新的用户,我很有可能只是忘记了某些事情)?观察者位于 controller_action_postdispatch 下的相关配置文件中,并且处于观察者模型中.让 setParam 正确运行的上下文是什么?

What about the Magento - Redirect Customer from Observer Method post am I not getting (there is a good chance I'm just oblivious to something due to being a fairly new user)? The observer is laid out in the relative config file under controller_action_postdispatch and is in an observer model. What is the context for getting setParam to operate correctly?

感谢大家的帮助!

推荐答案

我知道这篇文章有点旧,但我看到它已经为我自己的模块解决了.

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

我在 2 个不同的事件上运行重定向.当客户注册或客户登录时.

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.

对于 customer_register_success 事件,您需要做的就是将 success_url 参数设置为您想要的任何 url.

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.

对于 customer_login 事件,您会在控制器 customer/account/_loginPostRedirect 中注意到正在客户会话中设置重定向 URL.正如您在下面的第二个观察者方法中看到的那样,您需要做的就是获取客户会话并设置 URL.出于我的目的,我设置了 BeforeAuthUrl 和 AfterAuthUrl.

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

这篇关于Magento - 通过观察者登录时重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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