4

I am doing some customization work on cart page. I want to add product multiple times with same configuration during update items. Add to cart is working good but existing items get removed during update items. Here is the code, which I am using

I have overridden update controller ie. vendor/magento/module-checkout/Controller/Cart/UpdatePost.php and customize the _updateShoppingCart method.

foreach($cartCustomData as $key => $data) {
  $superAttributeData = $data['super_attribute'];
  $params = $data['custom_param'];
  $_product = $objectManager->create('Magento\Catalog\Model\Product')->load($data['product']);
  $this->cart->addProduct($_product,$updateParams);
}

$this->cart->save();

this works but when

$this->cart->updateItems($cartData)->save();

This is removing the exist one item.

Thanks in advance. Any help would be appreciated.

Updated :

protected function _updateShoppingCart()
    {
        try {
            $cartData = $this->getRequest()->getParam('cart');
            $cartCustomData = $this->getRequest()->getParam('cart_custom_data');

            if (is_array($cartData)) {
                $filter = new \Zend_Filter_LocalizedToNormalized(
                    ['locale' => $this->_objectManager->get(
                        \Magento\Framework\Locale\ResolverInterface::class
                    )->getLocale()]
                );
                foreach ($cartData as $index => $data) {
                    if (isset($data['qty'])) {
                        $cartData[$index]['qty'] = $filter->filter(trim($data['qty']));
                    }
                }
                if (!$this->cart->getCustomerSession()->getCustomerId() && $this->cart->getQuote()->getCustomerId()) {
                    $this->cart->getQuote()->setCustomerId(null);
                }

                //* custom code *//
                foreach($cartCustomData as $key => $data) {
                  $superAttributeData = $data['super_attribute'];
                  $params = $data['custom_param'];
                  $_product = $objectManager->create('Magento\Catalog\Model\Product')->load($data['product']);
                  $this->cart->addProduct($_product,$updateParams);
                  $this->cart->save();
                }
                //* custom code *//

                $cartData = $this->cart->suggestItemsQty($cartData);
                $this->cart->updateItems($cartData)->save();
            }
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            $this->messageManager->addError(
                $this->_objectManager->get(\Magento\Framework\Escaper::class)->escapeHtml($e->getMessage())
            );
        } catch (\Exception $e) {
            $this->messageManager->addException($e, __('We can\'t update the shopping cart.'));
            $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
        }
    }
  • Hi Zed, Can you please update the question with whole _updateShoppingCart() function which you have overridden. – Ramkishan Suthar Sep 21 '18 at 14:35
  • @Ramkishan, Updated the question. Please check. – zed Blackbeard Sep 21 '18 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.