3

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

  • remove var/generation folder and try again – Priyank Aug 3 '18 at 5:48
4

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'); 
        echo $cartId=$cart->getQuote()->getId();

        $itemId = 720;
        $itemQty = 1;

        $quote = $this->quoteRepository->getActive($cartId);
        $cartitems = $cart->getQuote()->getAllItems();
        $cartitems->setquoteId($cartId);
        $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
  • 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 Aug 3 '18 at 6:42
  • @RaviKant , in your execute method you have not defined cartitems in and directly used at here $cartitems->setquoteId($cartId); – Murtuza Zabuawala Aug 3 '18 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 Aug 3 '18 at 7:00
  • @RaviKant check this link magento.stackexchange.com/questions/111137/… – Murtuza Zabuawala Aug 3 '18 at 7:04
  • 2
    @Vivek , it will not update minicart – Murtuza Zabuawala Dec 28 '18 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

  • 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 Aug 3 '18 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();

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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