issue with add product to cart magento 2
-
14-04-2021 - |
Question
I am trying to add a new configurable product with it's supper attributes when we hit the update cart button.
check code here
/**
* Update customer's shopping cart
*
* @return void
*/
protected function _updateShoppingCart()
{
try {
$cartData = $this->getRequest()->getParam('cart');
if (is_array($cartData)) {
if (!$this->cart->getCustomerSession()->getCustomerId() && $this->cart->getQuote()->getCustomerId()) {
$this->cart->getQuote()->setCustomerId(null);
}
$cartData = $this->quantityProcessor->process($cartData);
$cartData = $this->cart->suggestItemsQty($cartData);
$this->addnewproducttocart();
$this->cart->updateItems($cartData)->save(); // second point
}
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->messageManager->addErrorMessage(
$this->_objectManager->get(\Magento\Framework\Escaper::class)->escapeHtml($e->getMessage())
);
} catch (\Exception $e) {
$this->messageManager->addExceptionMessage($e, __('We can\'t update the shopping cart.'));
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
}
}
public function addnewproducttocart(){
$product_id = '126';
$product = $this->_objectManager->create('Magento\Catalog\Model\Product')->load($productId);
$params = array(
'product' => $product_id,
'super_attribute' => array(
525 => 100,
//525 is the attribute id of size and 100 is the selected option value (small) of that attribute.
),
'qty' => 2,
);
$this->cart->addProduct($product, $params);
$this->cart->save(); // first point
}
This was working before in Magento 2.2.2 but after upgrade to Magento 2.3.4 not working correctly.
The problem is when we save cart ( at the second point ) it deletes the existing quote item and creates new also info_buyrequest is missing for a new one.
Working fine if we write exit after the first point when saving cart but creating a problem when we save cart at the second point.
If you guys have any Idea please share.
Any help will be appreciated.
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange