Please let me know how to add product to cart programmatically for custom guest user quote using an event observer in Magento 2
I try to add product for the registered user it was added successfully but when i added product for guest user in quote it was added with 0 base_price on checkout_cart_save_after event.
Here is the code
$quote = $this->checkoutSession->getQuote();
$items = $quote->getAllVisibleItems();
$newQuote = $this->quoteFactory->create(); //MagentoQuoteModelQuoteFactory;
$store = $this->storeManager->getStore();
$storeId = $store->getStoreId();
$newQuote->setStore($store);
$newQuote->setCustomerIsGuest(1);
//$customerid = $quote->getCustomerId();
//if($customerid){
// $customer = $this->customerRepository->getById($customerid);
// $newQuote->assignCustomer($customer); // }
foreach ($items as $item)
{
$productId = $item->getProductId();
$_product=$this->productRepository->getById($item['product_id']);
$newQuote->addProduct($_product,intval($item['qty']));
$newQuote->collectTotals()->save();
}