Magento 2 : Updating quote item
I am not sure about my below solution:
- May be you need to load Quote item object by item id
- As you want set price then using setPrice() you cannot set your desire price for that cart item
$item = $quote->getItemById($item->getId()); if (!$item) { continue; } $item->setQty((double) $qty); $item->setCustomPrice($price); $item->setOriginalCustomPrice($price); $item->getProduct()->setIsSuperMode(true); $item->save();
The functionality you are asking about is already done in Magento\Checkout\Controller\Cart\UpdatePost
which is executed when we update cart on cart page of magento.
It runs updateItems()
function of Magento\Checkout\Model\Cart
to update items in quote_item
table . It take parameter in the form of array $data ['item id of product']['attribute you want to update']
.
So , you can call updateItems()
on cart model object and pass the data accordingly to update items.Also if you are not getting item id you can get it like $this->cart->getQuote()->getAllItems()
and then call getItemId()
on each item.