cart->truncate()->save(); } catch (\Magento\Framework\Exception\LocalizedException $exception) { $this->messageManager->addError($exception->getMessage()); } catch (\Exception $exception) { $this->messageManager->addException($exception, __('We can\'t update the shopping cart.')); } } /** * Update customer's shopping cart * * @return void */ protected function _updateShoppingCart() { try { $cartData = $this->getRequest()->getParam('cart'); if (is_array($cartData)) { $filter = new \Zend_Filter_LocalizedToNormalized( ['locale' => $this->_objectManager->get('Magento\Framework\Locale\ResolverInterface')->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); } $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')->escapeHtml($e->getMessage()) ); } catch (\Exception $e) { $this->messageManager->addException($e, __('We can\'t update the shopping cart.')); $this->_objectManager->get('Psr\Log\LoggerInterface')->critical($e); } } /** * Update shopping cart data action * * @return \Magento\Framework\Controller\Result\Redirect */ public function execute() { if (!$this->_formKeyValidator->validate($this->getRequest())) { return $this->resultRedirectFactory->create()->setPath('*/*/'); } $updateAction = (string)$this->getRequest()->getParam('update_cart_action'); switch ($updateAction) { case 'empty_cart': $this->_emptyShoppingCart(); break; case 'update_qty': $this->_updateShoppingCart(); break; default: $this->_updateShoppingCart(); } return $this->_goBack(); } }