It is not possible to change the response in the Magento event observer, although the observer shoots

I am trying to use an observer to change the response to an add to cart controller action, but only in the context of an AJAX request.

My observer is called , and my JS retrieves the data in order, I checked this by putting die()an observer in my function cartAdd()and checking the answer developer console, which I use to see the result of my answer from Magento. Therefore, JS is not a problem here.

My main problem is that I cannot change the answer through normal functions. I get a request using $observer->getEvent()->getControllerAction()->getResponse(), and then make changes to it setHeader()or setBody()or any other function that modifies the response, but the response effect has absolutely no effect!

Does anyone know why I cannot change the answer in my observer?

In / 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>

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

Please note: I know that this code is not going to add AJAXify to the cart at all (this is my final goal). At the moment, I'm just trying to solve this problem.

In the end, I just get the contents of the page that you will get as a result of the add to cart action:

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

+5
source share
1 answer

, , . . a > > > : .

, controller_action_predispatch_checkout_cart_add; ref. Mage_Checkout_CartController::addAction(). ! Magento , Mage_Checkout, , checkout/session . , , , - .

addAction() addAction() checkout_cart_add_product_complete. Mage_Wishlist. Mage_Wishlist_Model_Observer::processAddToCart() , 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, controller_front_send_response_before.

+8

All Articles