即使观察者正在触发,也无法修改Magento事件观察器中的响应

时间:2012-09-14 22:13:12

标签: magento magento-1.7

我试图使用观察者修改添加到购物车控制器操作的响应,但仅限于AJAX请求的上下文。

我的观察者调用,我的JS正在检索数据,我通过在我的观察者函数die()中放置一个cartAdd()并验证响应开发者控制台来验证这一点,我用它来看看我对Magento的回应结果。所以JS不是这里的问题。

我的主要问题是我似乎无法通过正常功能修改响应。我使用$observer->getEvent()->getControllerAction()->getResponse()获取请求,然后通过setHeader()setBody()或任何其他修改响应的函数对其进行更改,但对响应完全没有影响!

有没有人知道为什么我无法修改观察者的反应?

在/app/code/local/mynamespace/mymodule/etc/config.xml中:

<frontend>
....
    <events>
        <controller_action_predispatch_checkout_cart_add>
            <observers>
                <mymodule_cart_add>
                    <type>singleton</type>
                    <class>mymodule/observer</class>
                    <method>cartAdd</method>
                </mymodule_cart_add>
            </observers>
        </controller_action_predispatch_checkout_cart_add>
    </events>
</frontend>

在/app/code/local/mynamespace/mymodule/Model/Observer.php中:

public function cartAdd(Varien_Event_Observer $observer)
{
    $controllerAction = $observer->getEvent()->getControllerAction();
    if($controllerAction->getRequest()->isAjax()) {
        $response = $controllerAction->getResponse();
        // I've even tried using:
        // $response = Mage::app()->getResponse();
        $response->setHeader('HTTP/1.1','403 Forbidden'); //using this because i will need it in my final code and it will make it immediatly obvious the response has been changed
        $response->setHeader('Content-type', 'application/json');
        $response->setBody('hello world!!!!');
        // this is to stop the product from being added to the cart
        $controllerAction->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
    }
}

请注意:我知道此代码并非适用于所有添加到购物车的AJAXify(这是我的最终目标)。目前我只是想解决这个问题

我最终只是获取了由于运行添加到购物车操作而最终会显示的页面内容:

An example of the data I am seeing in the console, as shown to me by my JS code

1 个答案:

答案 0 :(得分:8)

将产品添加到购物车时,可以通过管理员配置行为将人员发送到购物车页面或将其重定向回产品页面。请参阅系统&gt;配置&gt;结帐&gt;购物车:将产品重定向添加到购物车字段后。

此重定向行为是通过重定向完成的,该重定向将取代动态controller_action_predispatch_checkout_cart_add事件中的任何重定向集;来自Mage_Checkout_CartController::addAction()的{​​{3}}。不过没有恐惧! Magento核心开发人员也需要覆盖这种行为,因此如果已经设置了标志,则可以通知 Mage_Checkout 购物车控制器的addAction()方法绕过正常的重定向行为。 checkout/session对象。不仅有一个钩子和支持逻辑使它工作,但实际上有一个来自核心的工作示例 - 对开发人员来说总是一件好事。

addAction()方法中的最终重定向逻辑之前,购物车控制器的addAction()方法调度ref. the final bit of logic事件。 Mage_Wishlist 观察员checkout_cart_add_product_complete。从Mage_Wishlist_Model_Observer::processAddToCart()方法快速查看This event is observed可显示如何防止购物车控制器的addAction()方法重定向 - 即在no_cart_redirect对象上设置checkout/session标记,它保留了响应对象上的重定向集。

在这种情况下还有一个考虑因素。可能应保留 Mage_Wishlist 观察员行为,即:在从愿望清单向购物车添加产品后,客户可能会被重定向到其愿望清单中的下一个产品。这是观察员处理顺序重要的一个例子。为确保 Mage_Wishlist 模块的添加到购物车行为得到保留,消耗checkout_cart_add_product_complete事件的其他模块应在 Mage_Wishlist 观察者之前触发。在自定义模块的声明文件中,Mage_Wishlist模块应设置为依赖于自定义模块,这将确保自定义模块的观察者将在 Mage_Wishlist 模块之前触发:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Custom_Module>
            <active>true</active>
            <codePool>local</codePool>
        </Custom_Module>
        <Mage_Wishlist>
            <depends>
                <Custom_Module />
            </depends>
        </Mage_Wishlist>
    </modules>
</config>

如果 Mage_Wishlist 模块不是一个因素,那么更有针对性的事件就是动态生成的controller_action_postdispatch_checkout_cart_add事件,这是通用{{1}之前的最后一个目标事件事件。

相关问题