Magento – Magento2 : Which Magento Event for Update cart quantity only

cartevent-observermagento2.3quote

I have work on the task in which I need to handle quote item,
here I have use two event

checkout_cart_add_product_complete

checkout_cart_update_item_complete

here it work fine,
But when I have change Quantity directly in mini cart then my Logic was not work.
For this I am find some event which work for me.
If You have any solution this Pleas Provide Me

my code is

<event name="checkout_cart_add_product_complete">
    <observer name="customprice" instance="Ns\Module\Observer\CustomPrice" />
</event>
<event name="checkout_cart_update_item_complete">
    <observer name="custompriceupdate" instance="Ns\Module\Observer\CustomPrice" />
</event>
<event name="sales_quote_item_qty_set_after">
    <observer name="custompriceupdateqty" instance="Ns\Module\Observer\CustomPrice" />
</event>

and observer is

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $cart = $this->cart;
    $quote = $cart->getQuote();
    $items = $cart->getQuote()->getAllItems();
    $basetotal = 0;
    foreach ($items as $itemforprice) {
        $total = $this->processItem($itemforprice);
        $basetotal += $total;
    }

    $quote->setSubtotal($basetotal);
    $quote->setGrandTotal($basetotal);
    $quote->setBaseSubtotal($basetotal);
    $quote->setBaseGrandTotal($basetotal);
    $quote->setSubtotalWithDiscount($basetotal);
    $quote->setBaseSubtotalWithDiscount($basetotal);
    $quote->save();
}

private function processItem($itemforprice)
{
    $myprice = 100;
    $total = $myprice * $qty;

    $itemforprice->setCustomPrice($myprice);
    $itemforprice->setOriginalCustomPrice($myprice);
    $itemforprice->setBasePrice($myprice);
    $itemforprice->setPrice($myprice);

    $itemforprice->setRowTotal($total);
    $itemforprice->setBaseRowTotal($total);
    $itemforprice->setRowTotalInclTax($total);
    $itemforprice->setBaseRowTotalInclTax($total);

    $itemforprice->save();

    return $total;
}

Best Answer

Use sales_quote_item_qty_set_after which will fire when Qty is update.

 $this->_eventManager->dispatch('sales_quote_item_qty_set_after', ['item' => $this]);

I guess that you want to update cart item price.

sales_quote_item_qty_set_after event provide quote item as it parameter.So you don't need to inject cart object $this->cart here. Also, don't need to save the quote and calculate the subtotal, as Magento automatically do that during firing on this event.

<?php

namespace Ns\Module\Observer;

class CustomPriceSet  implements \Magento\Framework\Event\ObserverInterface
{

    public function execute(\Magento\Framework\Event\Observer $observer)
    {

    $item = $observer->getEvent()->getItem();
    $quote = $item->getQuote();  
        $myprice = 100;
        $item->setCustomPrice($myprice);
        $item->setOriginalCustomPrice($myprice);
        $item->getProduct()->setIsSuperMode(true);
    }

}