4

Magento2 - I am doing some customisation 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);
        }
    }
2
  • Hi Zed, Can you please update the question with whole _updateShoppingCart() function which you have overridden. Commented Sep 21, 2018 at 14:35
  • @Ramkishan, Updated the question. Please check. Commented Sep 21, 2018 at 15:02

1 Answer 1

0

Please see the core function from Magento 2.4.4

/**
 * 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->cart->updateItems($cartData)->save();
        }
    } 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);
    }
}

You can use console log via pressing F12 on browser and check some error requests on Network tab (click error row, then click preview tab).

I got an error on the Cart page: We can't update the shopping cart when trying update items qty. Reason 1: vendor/magento/module-checkout/Controller/Cart/UpdatePost.php #90

Reason 2: estimate-shipping-method 500 error. Error on Php 8.1 "Deprecated Functionality: strtolower(): Passing null to parameter #1 ($string) of type string is deprecated in /app/code/Magecomp/Matrixrate/Model/ResourceModel/Carrier/RateQuery.php on line 48"

Reason 3: section-details 400 error. Error on Aheadworks_OneStepCheckout extension. "Unable to retrieve payment methods. Please check input data."

#0 /app/code/Aheadworks/OneStepCheckout/Model/CheckoutSectionsManagement.php(177): Aheadworks\\OneStepCheckout\\Model\\CheckoutSectionsManagement->getPaymentMethods()
#1 /app/code/Aheadworks/OneStepCheckout/Model/GuestCheckoutSectionsManagement.php(49): Aheadworks\\OneStepCheckout\\Model\\CheckoutSectionsManagement->getSectionsDetails()
#2 [internal function]: Aheadworks\\OneStepCheckout\\Model\\GuestCheckoutSectionsManagement->getSectionsDetails()
#3 /vendor/magento/module-webapi/Controller/Rest/SynchronousRequestProcessor.php(95): call_user_func_array()
#4 /vendor/magento/module-webapi/Controller/Rest.php(188): Magento\\Webapi\\Controller\\Rest\\SynchronousRequestProcessor->process()
#5 /vendor/magento/framework/Interception/Interceptor.php(58): Magento\\Webapi\\Controller\\Rest->dispatch()
#6 /vendor/magento/framework/Interception/Interceptor.php(138): Magento\\Webapi\\Controller\\Rest\\Interceptor->___callParent()
#7 /vendor/fastly/magento2/Model/FrontControllerPlugin.php(128): Magento\\Webapi\\Controller\\Rest\\Interceptor->Magento\\Framework\\Interception\\{closure}()
#8 /vendor/magento/framework/Interception/Interceptor.php(135): Fastly\\Cdn\\Model\\FrontControllerPlugin->aroundDispatch()
#9 /vendor/magento/framework/Interception/Interceptor.php(153): Magento\\Webapi\\Controller\\Rest\\Interceptor->Magento\\Framework\\Interception\\{closure}()
#10 /generated/code/Magento/Webapi/Controller/Rest/Interceptor.php(23): Magento\\Webapi\\Controller\\Rest\\Interceptor->___callPlugins()
#11 /vendor/magento/framework/App/Http.php(116): Magento\\Webapi\\Controller\\Rest\\Interceptor->dispatch()
#12 /vendor/magento/framework/App/Bootstrap.php(264): Magento\\Framework\\App\\Http->launch()
#13 /pub/index.php(30): Magento\\Framework\\App\\Bootstrap->run()
#14 {main}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.