Delete.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Controller\Cart;
  7. use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
  8. /**
  9. * Action Delete.
  10. *
  11. * Deletes item from cart.
  12. */
  13. class Delete extends \Magento\Checkout\Controller\Cart implements HttpPostActionInterface
  14. {
  15. /**
  16. * Delete shopping cart item action
  17. *
  18. * @return \Magento\Framework\Controller\Result\Redirect
  19. */
  20. public function execute()
  21. {
  22. if (!$this->_formKeyValidator->validate($this->getRequest())) {
  23. return $this->resultRedirectFactory->create()->setPath('*/*/');
  24. }
  25. $id = (int)$this->getRequest()->getParam('id');
  26. if ($id) {
  27. try {
  28. $this->cart->removeItem($id);
  29. // We should set Totals to be recollected once more because of Cart model as usually is loading
  30. // before action executing and in case when triggerRecollect setted as true recollecting will
  31. // executed and the flag will be true already.
  32. $this->cart->getQuote()->setTotalsCollectedFlag(false);
  33. $this->cart->save();
  34. } catch (\Exception $e) {
  35. $this->messageManager->addErrorMessage(__('We can\'t remove the item.'));
  36. $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
  37. }
  38. }
  39. $defaultUrl = $this->_objectManager->create(\Magento\Framework\UrlInterface::class)->getUrl('*/*');
  40. return $this->resultRedirectFactory->create()->setUrl($this->_redirect->getRedirectUrl($defaultUrl));
  41. }
  42. }