Programatically update cart item quantity
-
21-01-2021 - |
سؤال
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();
}
}
المحلول
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
نصائح أخرى
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
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();
}
<?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.
لا تنتمي إلى magento.stackexchange