Quantcast
Channel: Webkul Blog
Viewing all articles
Browse latest Browse all 5488

Programmatically Add More Product To An Existing Quote In Magento 2

$
0
0

Programmatically Add More Product To An Existing Quote In Magento 2:- In this blog, I’ll explain you that 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
$quoteId = 10; // here give quote id

$product = $this->productRepository->getById($productId); // _productRepository is an instance of \Magento\Catalog\Api\ProductRepositoryInterface
$cartItem = $this->cartItem;  // cartItem is an instance of Magento\Quote\Api\Data\CartItemInterface

// set product sku to cart item
$cartItem->setSku($product->getSku());

// assign quote Id to cart item
$cartItem->setQuoteId($quoteId);

// set product Quantity
$cartItem->setQty($qty);

// add ptoduct to cart
$newItem = $this->itemRepository->save($cartItem); // it will save cart item and return newly added Item

 

In this way we can add simple product to an existing quote. Hope it will be helpful for you. 🙂


Viewing all articles
Browse latest Browse all 5488

Trending Articles