Magento-从观察者方法重定向客户 [英] Magento - Redirect Customer from Observer Method

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

问题描述

在此事件checkout_cart_add_product_complete中,我希望将客户重定向到外部网页http://www.example.com/.为此,我正在使用此代码,它根本不起作用:-

In this event checkout_cart_add_product_complete, I want the customer to be redirected to an external web page http://www.example.com/. For this I am using this code, which is not working at all:-

public function moduleMethod() {
    /* @var $response1 Mage_Core_Controller_Response_Http */
    $response1 = $observer->getEvent()->getResponse();

    /* @var $response2 Mage_Core_Controller_Response_Http */
    $response2 = Mage::app()->getResponse();

    $url = 'http://www.example.com/';
    $response1->setRedirect($url);

    return;
}

我在这两个变量"$response1"和"$response2"上都使用了"setRedirect()"方法,但是它们都向我显示了购物车页面,而我想看到此页面http://www.example.com/代替.

I have used the "setRedirect()" method on both these variables "$response1" and "$response2", but both of them show me the Shopping Cart page, whereas I want to see this page http://www.example.com/ instead.

我想要什么:

  • 当我可以有效地使用事件观察器过程时,我不想重写控制器类,而只是为了重定向客户.
  • 当Magento框架以有效方式提供此功能时,我不想使用PHP内置函数"header()".

推荐答案

tl; dr:在底部纠正观察者代码.

tl;dr: Correct observer code at the bottom.

我回答之前的注释:确保观察者已被触发;单步执行代码或使用die('here');.按照书面说明,您的示例方法没有正确的原型来接收事件观察者数据(缺少参数).

A note before I answer: Make sure that the observer is being triggered; step through your code or use die('here');. As written, your example method does not have the correct prototype to receive event observer data (missing an argument).

在这种情况下,使用事件观察器进行重定向逻辑是完全合适的,正如核心团队将请求和响应对象显式传递给观察器所证明的那样.您的尝试很好,但是我认为您有一些条件和配置会导致执行流向Mage_Checkout_CartController::_goBack(),尤其是行

Using an event observer for redirect logic is totally appropriate in this context, as evinced by the core team explicitly passing the request and response objects in to the observer. Your attempt is good, but I think that you have conditions and configuration which cause execution to flow to Mage_Checkout_CartController::_goBack(), specifically to the line

$this->_redirect('checkout/cart');

因此,我们需要修改我们的方法.现在,您可以通过操纵响应并调用前端控制器的sendResponse()方法来阻止事件观察者之后处理任何请求/响应逻辑,如下所示( nb:不要这样做!):

So we need to revise our approach. Now, you could prevent any request/response logic from processing after your event observer by manipulating the response and calling the Front Controller's sendResponse() method as demonstrated below (nb: don't do this!):

public function moduleMethod($observer) //note I added a param
{
    /* @var $response1 Mage_Core_Controller_Response_Http */
    $response1 = $observer->getResponse(); // observers have event args

    $url = 'http://www.example.com/';
    $response1->setRedirect($url);

    /* SHOULDN'T DO THIS */
    Mage::app()->getFrontController()->sendResponse();
}

这应该起作用,但是我认为它通过触发以太系统组件(EDA)的输出来混合令人关注的领域.让我们看看命令控制结构中是否可以使用...

This should work, but I think it mixes up areas of concern by triggering output from an ethereal system component (EDA). Let's see if there's something in the command-control structure which we can use...

checkout_cart_add_product_complete事件执行之后,才进入购物车控制器的_goBack()方法.此方法名称的问题在于它的作用超出了其名称所暗示的范围:

Just after the checkout_cart_add_product_complete event execution comes to the cart controller's _goBack() method. The problem with this method name is that it does more than its name implies:

/**
 * Set back redirect url to response
 *
 * @return Mage_Checkout_CartController
 */
protected function _goBack()
{
    $returnUrl = $this->getRequest()->getParam('return_url');
    if ($returnUrl) {
        // clear layout messages in case of external url redirect
        if ($this->_isUrlInternal($returnUrl)) {
            $this->_getSession()->getMessages(true);
        }
        $this->getResponse()->setRedirect($returnUrl);
    }
    //...
}

看起来我们可以在请求对象上设置一个return_url参数,并完成我们需要的操作.

It looks like we can just set a return_url param on the request object and accomplish what we need.

public function moduleMethod(Varien_Event_Observer $observer)
{
    $observer->getRequest()->setParam('return_url','http://www.google.com/');
}

我已经测试过了,应该可以解决问题!

I've tested this, and it should do the trick!

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

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