2

I have an observer which listens to the checkout_cart_add_product_complete event in Magento (this works correctly) but when I try to access the request data returned by the dispatched event it doesn't seem to work as expected.

The source code which dispatches the event can be found here, lines 117-120 dispatch the event and you can see the request parameter is present in the dispatched data.

The same source code uses the same request to get the qty param, which for example is present in my request data.

This is how I am accessing the data dispatched by that event:

public function execute(\Magento\Framework\Event\Observer $observer) {
    $event = $observer->getEvent();
    $product = $event->getData('product');
    $request = $event->getData('request');

    $params = $request->getParams();

    $this->myhelpername->log("PARAMS: " . var_export($params, true));

Which returns:

PARAMS: array (
  'uenc' => 'aHR0cDovL21hZ2VudG8uaW50aWxlcnkuY29tL2NhdGFsb2dzZWFyY2gvcmVzdWx0Lz9xPXRlc3Q,',
  'product' => '2',
  'form_key' => 'F6GFMhoJHPpeoOaa',
) [] []

As you can see this is clearly not the same request returned as it doesn't contain the qty value. You can see this is access in the source code on lines 86-94.

Any idea why this is?

Note: If I inject my own Request listener and do $params = $this->request->getParams(); (where request is an instance of \Magento\Framework\App\RequestInterface then this prints the exact same data as using the above which makes me think I am doing something wrong.

1

By default, when we are in product listing, trying to add product to cart, this event is trigger. However, we cannot get the qty from request. Magento will add product with quantity 1.

enter image description here

If adding a product in product detail page, we have a qty box. If we try to get the quantity from the controller and observer, we can get the values.

$params = $this->getRequest()->getParams();

$observer->getEvent()->getRequest()->getParams();

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.