2

I tried to get the update data when cart item qty is updated. I used event checkout_cart_update_items_before because i want to check it before the item updated. How can i get the update data?

Example data i want to take :

 1. Item A change qty from 10 to 5.
 2. Item B change qty from 1 to 3.
2

You can use data parameters of the same event checkout_cart_update_items_before to get the desired result.

There are two parameters being dispatched with this event. One is the actual cart object and the second one holds the updated data.

Both objects have a relationship based on quote item id. You can use that to get your result like below.

app/code/{Namespace}/{Module}/etc/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="checkout_cart_update_items_before">
        <observer name="update_item_before" instance="{Namespace}\{Module}\Observer\UpdateCartItem" />
    </event>
</config>

app/code/{Namespace}/{Module}/Observer/UpdateCartItem.php

<?php
namespace {Namespace}\{Module}\Observer;

class UpdateCartItem implements \Magento\Framework\Event\ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $items = $observer->getCart()->getQuote()->getItems();
        $info = $observer->getInfo()->getData();

        foreach ($items as $item) {
             echo 'Item '. $item->getName() .' change qty from ' . $item->getQty() . ' to ' . $info[$item->getId()]['qty'] . '.<br>';
        }
    }
}

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.