Programmatically Add More Product To An Existing Quote In Magento 2:- In this blog, I’ll explain how we can add a simple or virtual product to an existing quote.
$productId = 12; // assign product Id $qty = 2; // assign product quantity which you want to add //Get quote from checkout session $session = $this->checkoutSession->create(); //checkoutSession is an instance of \Magento\Checkout\Model\SessionFactory $quote = $session->getQuote(); $product = $this->productRepository->getById($productId); // _productRepository is an instance of \Magento\Catalog\Api\ProductRepositoryInterface $quote->addProduct($product, $qty); $this->cartRepository->save($quote); //cartRepository is an instance of \Magento\Quote\Api\CartRepositoryInterface $session->replaceQuote($quote)->unsLastRealOrderId();
For Simple Type product with custom option you can check : Programmatically Add More Product With Custom Options To An Existing Quote In Magento 2
In this way we can add simple product to an existing quote. Hope it will be helpful for you. 🙂
Be the first to comment.