UpdatePost.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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\Checkout\Model\Cart\RequestQuantityProcessor;
  8. use Magento\Framework\App\Action\HttpPostActionInterface;
  9. use Magento\Framework\App\Action\HttpGetActionInterface;
  10. /**
  11. * Post update shopping cart.
  12. */
  13. class UpdatePost extends \Magento\Checkout\Controller\Cart implements HttpGetActionInterface, HttpPostActionInterface
  14. {
  15. /**
  16. * @var RequestQuantityProcessor
  17. */
  18. private $quantityProcessor;
  19. /**
  20. * @param \Magento\Framework\App\Action\Context $context
  21. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  22. * @param \Magento\Checkout\Model\Session $checkoutSession
  23. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  24. * @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
  25. * @param \Magento\Checkout\Model\Cart $cart
  26. * @param RequestQuantityProcessor $quantityProcessor
  27. */
  28. public function __construct(
  29. \Magento\Framework\App\Action\Context $context,
  30. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  31. \Magento\Checkout\Model\Session $checkoutSession,
  32. \Magento\Store\Model\StoreManagerInterface $storeManager,
  33. \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
  34. \Magento\Checkout\Model\Cart $cart,
  35. RequestQuantityProcessor $quantityProcessor = null
  36. ) {
  37. parent::__construct(
  38. $context,
  39. $scopeConfig,
  40. $checkoutSession,
  41. $storeManager,
  42. $formKeyValidator,
  43. $cart
  44. );
  45. $this->quantityProcessor = $quantityProcessor ?: $this->_objectManager->get(RequestQuantityProcessor::class);
  46. }
  47. /**
  48. * Empty customer's shopping cart
  49. *
  50. * @return void
  51. */
  52. protected function _emptyShoppingCart()
  53. {
  54. try {
  55. $this->cart->truncate()->save();
  56. } catch (\Magento\Framework\Exception\LocalizedException $exception) {
  57. $this->messageManager->addErrorMessage($exception->getMessage());
  58. } catch (\Exception $exception) {
  59. $this->messageManager->addExceptionMessage($exception, __('We can\'t update the shopping cart.'));
  60. }
  61. }
  62. /**
  63. * Update customer's shopping cart
  64. *
  65. * @return void
  66. */
  67. protected function _updateShoppingCart()
  68. {
  69. try {
  70. $cartData = $this->getRequest()->getParam('cart');
  71. if (is_array($cartData)) {
  72. if (!$this->cart->getCustomerSession()->getCustomerId() && $this->cart->getQuote()->getCustomerId()) {
  73. $this->cart->getQuote()->setCustomerId(null);
  74. }
  75. $cartData = $this->quantityProcessor->process($cartData);
  76. $cartData = $this->cart->suggestItemsQty($cartData);
  77. $this->cart->updateItems($cartData)->save();
  78. }
  79. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  80. $this->messageManager->addErrorMessage(
  81. $this->_objectManager->get(\Magento\Framework\Escaper::class)->escapeHtml($e->getMessage())
  82. );
  83. } catch (\Exception $e) {
  84. $this->messageManager->addExceptionMessage($e, __('We can\'t update the shopping cart.'));
  85. $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
  86. }
  87. }
  88. /**
  89. * Update shopping cart data action
  90. *
  91. * @return \Magento\Framework\Controller\Result\Redirect
  92. */
  93. public function execute()
  94. {
  95. if (!$this->_formKeyValidator->validate($this->getRequest())) {
  96. return $this->resultRedirectFactory->create()->setPath('*/*/');
  97. }
  98. $updateAction = (string)$this->getRequest()->getParam('update_cart_action');
  99. switch ($updateAction) {
  100. case 'empty_cart':
  101. $this->_emptyShoppingCart();
  102. break;
  103. case 'update_qty':
  104. $this->_updateShoppingCart();
  105. break;
  106. default:
  107. $this->_updateShoppingCart();
  108. }
  109. return $this->_goBack();
  110. }
  111. }