Back to Top

Programmatically Add More Product To An Existing Quote In Magento 2

Updated 28 February 2024

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

Searching for an experienced
Magento 2 Company ?
Find out More
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home