4

Below is my code:

namespace Ves\Productlist\Controller\Cart;
use Magento\Quote\Model\QuoteRepository;
class CartUpdate extends \Magento\Framework\App\Action\Action
{
    protected $quoteRepository;

    public function __construct(
    \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
    ) {
    $this->quoteRepository = $quoteRepository;
    }

    public function execute()
    {
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $cart = $objectManager->get('\Magento\Checkout\Model\Cart'); 
echo $cartId=$cart->getQuote()->getId();

    $itemId = 720;
    $itemQty = 1;

    $quote = $this->quoteRepository->getActive($cartId);
    $cartitems->setquoteId($cartId);
    $cartitems->setitemId($itemId);
    $cartitems->setqty($itemQty);

    $quoteItems[] = $cartitems;
    $quote->setItems($quoteItems);
    $this->quoteRepository->save($quote);
    $quote->collectTotals();

    }

}

But I am getting the below error: enter link description here

1
  • remove var/generation folder and try again
    – Priyank
    Commented Aug 3, 2018 at 5:48

5 Answers 5

6

Change Your construct as per below code :

namespace Ves\Productlist\Controller\Cart;

use Magento\Quote\Model\QuoteRepository;

class CartUpdate extends \Magento\Framework\App\Action\Action
{
    protected $quoteRepository;


    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
    ) {
        $this->quoteRepository = $quoteRepository;
        parent::__construct($context);
    }

    public function execute()
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $cart = $objectManager->get('\Magento\Checkout\Model\Cart'); 
        $quoteId = $cart->getQuote()->getId();

        $itemId = 720;
        $itemQty = 1;

        $quote = $this->quoteRepository->getActive($quoteId);
        $cartitems = $cart->getQuote()->getAllItems();
        $cartitems->setQuoteId($quoteId);
        $cartitems->setItemId($itemId);
        $cartitems->setQty($itemQty);

        $quoteItems[] = $cartitems;
        $quote->setItems($quoteItems);
        $this->quoteRepository->save($quote);
        $quote->collectTotals();

    }

}

After changing your code go to terminal and fire the below command.

cd /yourMagentoPath
php bin/magento setup:di:compile
7
  • getting new error " Exception #0 (Exception): Notice: Undefined variable: cartitems in /var/www/html/halalrush/app/code/Ves/Productlist/Controller/Cart/CartUpdate.php on line 30 "
    – Ravi Kant
    Commented Aug 3, 2018 at 6:42
  • @RaviKant , in your execute method you have not defined cartitems in and directly used at here $cartitems->setquoteId($cartId); Commented Aug 3, 2018 at 6:44
  • yes i did not defined cartitem but how can we define cartitems here and how to update cart item's quantity,
    – Ravi Kant
    Commented Aug 3, 2018 at 7:00
  • @RaviKant check this link magento.stackexchange.com/questions/111137/… Commented Aug 3, 2018 at 7:04
  • 2
    @Vivek , it will not update minicart Commented Dec 28, 2018 at 8:50
0

Try this Code

namespace Ves\Productlist\Controller\Cart;
use Magento\Quote\Model\QuoteRepository;
class CartUpdate extends \Magento\Framework\App\Action\Action
{
    protected $quoteRepository;

    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
    ) {
        parent::__construct($context);
        $this->quoteRepository = $quoteRepository;
    }

    public function execute()
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $cart = $objectManager->get('\Magento\Checkout\Model\Cart'); 
        echo $cartId=$cart->getQuote()->getId();

        $itemId = 720;
        $itemQty = 1;

        $quote = $this->quoteRepository->getActive($cartId);
        $cartitems->setquoteId($cartId);
        $cartitems->setitemId($itemId);
        $cartitems->setqty($itemQty);

        $quoteItems[] = $cartitems;
        $quote->setItems($quoteItems);
        $this->quoteRepository->save($quote);
        $quote->collectTotals();

    }

}

please remove generated and cache after check

1
  • Now getting new error " Exception #0 (Exception): Notice: Undefined variable: cartitems in /var/www/html/halalrush/app/code/Ves/Productlist/Controller/Cart/CartUpdate.php on line 30 "
    – Ravi Kant
    Commented Aug 3, 2018 at 6:41
0

public function __construct(
    \Magento\Backend\App\Action\Context $context,
    \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
) {
    parent::__construct($context);
    //setting quote repos into variable
    $this->quoteRepository = $quoteRepository;
}

public function execute()
{   
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    //populate cart class
    $cart = $objectManager->get('\Magento\Checkout\Model\Cart'); 
    echo $cartId=$cart->getQuote()->getId();

    //setting item id and qty for cart 
    $itemId = 12;
    $itemQty = 2;

    $quote = $this->quoteRepository->getActive($cartId);
    $cartitems->setquoteId($cartId);
    $cartitems->setitemId($itemId);
    $cartitems->setqty($itemQty);

    $quoteItems[] = $cartitems;
    $quote->setItems($quoteItems);
    $this->quoteRepository->save($quote);
    $quote->collectTotals();

}
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.

2
  • How can I update bundle and group item qty? Can you please help with that? Commented Dec 6, 2020 at 18:41
  • In group product all group product are show here as simple product not as group product. And for Bundle product you can show in above i set selected option and option qty. if bundle_option and bundle_option_qty key found. Please check Update product data part and apply.
    – Msquare
    Commented Dec 7, 2020 at 4:04
0
 public function __construct(        \Magento\Quote\Model\ResourceModel\Quote\Item\CollectionFactory $quoteItemCollectionFactory,
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository

)
{
        $this->quoteItemCollectionFactory = $quoteItemCollectionFactory;
        $this->quoteRepository = $quoteRepository;
}

 public function updateQty($itemId,$quoteId,$qty)
            {
                //calling the quote item collection
                $quoteItemCollection = $this->quoteItemCollectionFactory>create();
                $quoteItem  = $quoteItemCollection->addFieldToFilter('item_id',$itemId );
                foreach($quoteItem as $item) {   
                    $item->setQty($qty);  //setting the quantity in the table
                    $item->save();
                }
                $quote = $this->quoteRepository->getActive($cartId);
                $quote->collectTotals(); //refreshing the subtotal with modified quantity

            }

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.