I want to programmatically update the quantity of a bundle option of a item in the users cart.
Currently in the cart, a bundle with the option “12 x DIN A4 page”.
This is what I have done so far
/** @var \Magento\Quote\Model\Quote\Item */ protected $ quoteItem; public function updateNumberOfPages($ pages) { $ qtyOptions = $ this->quoteItem->getQtyOptions(); assert(count($ qtyOptions) == 1); $ option = reset($ qtyOptions); $ qty = $ pages * $ this->getNumberOfCopies(); $ this->quoteItem->updateQtyOption($ option, $ qty); $ this->quoteItem->save($ this); $ this->quoteItem->getQuote()->setTotalsCollectedFlag(true)->collectTotals(); return $ this; }
So I call this function updateNumberOfPages(4);
A bit later in the same request, I want to fetch and the new totals:
/** @var \Magento\Quote\Api\CartTotalRepositoryInterface */ protected $ cartTotalsRepository; ... $ totals = $ this->cartTotalsRepository->get($ quoteItem->getQuoteId());
But here, the just updated item still reports “12 x DIN A4 page” – even I have set the qty above to 4.
What is going wrong?
Bonus question: $ this->quoteItem->save()
is deprecated – what to use instead?