4

How to update quote item quantity programmatically in Magento 2

From Controller and which repository should i use ?

2 Answers 2

0

You can use the Quote repository \Magento\Quote\Api\CartRepositoryInterface

 $quote = $this->quoteRepository->getActive($QuoteId);
 $quoteItem = $quote->getItemById($itemId);
if (!$quoteItem) {
  continue;
}
$quoteItem->setQty((double) $qty);
$quoteItem->save();
$this->quoteRepository->save($quote);

At this case, you also need quote id also

DELETE a quote item from Quote

use Magento\Quote\Model\Quote\Item\Repository

and use deleteById($QuoteId, $itemId),method.

5
  • Thank you @Amit_Bera. Working Perfect can you please tell how to delete that item. $quoteItem->delete(); <- is this correct ? Jul 9, 2018 at 9:38
  • Please check the answer, i have updated the answer
    – Amit Bera
    Jul 9, 2018 at 11:45
  • @AmitBera: it's not updating cart item qty in Magento 2.3 Oct 9, 2019 at 10:41
  • okay. i will check
    – Amit Bera
    Oct 9, 2019 at 12:16
  • @sangeeta, Please check answer properly before edit the answer
    – Amit Bera
    Oct 9, 2019 at 12:50
0

Try this code enter image description here

<?php

namespace VendoreName\ModuleName\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\Controller\ResultFactory;

class Editcart extends \Magento\Framework\App\Action\Action
{
    protected $resultPageFactory;
    protected $request;
    protected $cart;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Checkout\Model\Cart $cart,
        \Magento\Framework\App\Request\Http $Request

    ) {
        $this->resultPageFactory = $resultPageFactory;
        $this->request = $request;
        $this->cart = $cart;
        parent::__construct($context);
    }

    public function execute()
    {
        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
        // get cart product data
        $data = $this->request->getParams();
        ///$data['quoteid'] is current qoute item id
        // $data['prdid'] is product id
        if (!isset($data['quoteid'])) {
            $this->messageManager->addError(__('Invalid product data.'));
            $resultRedirect->setUrl($this->_redirect->getRefererUrl());
            return $resultRedirect;
        }
        // get quote product by items id
        try {
            $quote_data = $this->cart->getQuote()->getItemById($data['quoteid']);

            if (!$quote_data) {
                throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t find the quote item.'));
            }

            $quote_params = $quote_data->getBuyRequest();

            // update product data
            if (isset($data['super_attribute'])) {
                $quote_params->setSuperAttribute($data['super_attribute'][$data['prdid']]);
            } elseif (isset($data['links'])) {
                $quote_params->setLinks($data['links']);
            } elseif ((isset($data['bundle_option'])) && (isset($data['bundle_option_qty']))) {
                $quote_params->setBundleOption($data['bundle_option']);
                $quote_params->setBundleOptionQty($data['bundle_option_qty']);
            } else {
                $quote_params = $quote_data->getBuyRequest();
            }

            //update custom options
            if (isset($data['options'])) {
                $quote_params->setOptions($data['options']);
            }

            // update qty
            $quote_params->setQty($data['prdqty']);

            // update product data with qty
            $item = $this->cart->getQuote()->updateItem($data['quoteid'], $quote_params);

            if (is_string($item)) {
                throw new \Magento\Framework\Exception\LocalizedException(__($item));
            }
            if ($item->getHasError()) {
                throw new \Magento\Framework\Exception\LocalizedException(__($item->getMessage()));
            }
            // save cart item
            $this->cart->save();
            $this->messageManager->addSuccessMessage(__('Product has been successfully updated.'));
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            $this->messageManager->addNoticeMessage($e->getMessage());
        } catch (Exception $e) {
            $this->messageManager->addExceptionMessage($e, __('We can\'t update the item right now.'));
        }
        $resultRedirect->setUrl($this->_redirect->getRefererUrl());
        return $resultRedirect;
    }
}

I Hope This Helps You.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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