UpdateItemQty.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Checkout\Controller\Cart;
  8. use Magento\Checkout\Model\Cart\RequestQuantityProcessor;
  9. use Magento\Framework\App\Action\Context;
  10. use Magento\Framework\Exception\LocalizedException;
  11. use Magento\Checkout\Model\Session as CheckoutSession;
  12. use Magento\Framework\Serialize\Serializer\Json;
  13. use Magento\Framework\Data\Form\FormKey\Validator as FormKeyValidator;
  14. use Magento\Quote\Model\Quote\Item;
  15. use Psr\Log\LoggerInterface;
  16. class UpdateItemQty extends \Magento\Framework\App\Action\Action
  17. {
  18. /**
  19. * @var RequestQuantityProcessor
  20. */
  21. private $quantityProcessor;
  22. /**
  23. * @var FormKeyValidator
  24. */
  25. private $formKeyValidator;
  26. /**
  27. * @var CheckoutSession
  28. */
  29. private $checkoutSession;
  30. /**
  31. * @var Json
  32. */
  33. private $json;
  34. /**
  35. * @var LoggerInterface
  36. */
  37. private $logger;
  38. /**
  39. * @param Context $context,
  40. * @param RequestQuantityProcessor $quantityProcessor
  41. * @param FormKeyValidator $formKeyValidator
  42. * @param CheckoutSession $checkoutSession
  43. * @param Json $json
  44. * @param LoggerInterface $logger
  45. */
  46. public function __construct(
  47. Context $context,
  48. RequestQuantityProcessor $quantityProcessor,
  49. FormKeyValidator $formKeyValidator,
  50. CheckoutSession $checkoutSession,
  51. Json $json,
  52. LoggerInterface $logger
  53. ) {
  54. $this->quantityProcessor = $quantityProcessor;
  55. $this->formKeyValidator = $formKeyValidator;
  56. $this->checkoutSession = $checkoutSession;
  57. $this->json = $json;
  58. $this->logger = $logger;
  59. parent::__construct($context);
  60. }
  61. /**
  62. * @return void
  63. */
  64. public function execute()
  65. {
  66. try {
  67. if (!$this->formKeyValidator->validate($this->getRequest())) {
  68. throw new LocalizedException(
  69. __('Something went wrong while saving the page. Please refresh the page and try again.')
  70. );
  71. }
  72. $cartData = $this->getRequest()->getParam('cart');
  73. if (!is_array($cartData)) {
  74. throw new LocalizedException(
  75. __('Something went wrong while saving the page. Please refresh the page and try again.')
  76. );
  77. }
  78. $cartData = $this->quantityProcessor->process($cartData);
  79. $quote = $this->checkoutSession->getQuote();
  80. foreach ($cartData as $itemId => $itemInfo) {
  81. $item = $quote->getItemById($itemId);
  82. $qty = isset($itemInfo['qty']) ? (double)$itemInfo['qty'] : 0;
  83. if ($item) {
  84. $this->updateItemQuantity($item, $qty);
  85. }
  86. }
  87. $this->jsonResponse();
  88. } catch (LocalizedException $e) {
  89. $this->jsonResponse($e->getMessage());
  90. } catch (\Exception $e) {
  91. $this->logger->critical($e->getMessage());
  92. $this->jsonResponse('Something went wrong while saving the page. Please refresh the page and try again.');
  93. }
  94. }
  95. /**
  96. * Updates quote item quantity.
  97. *
  98. * @param Item $item
  99. * @param float $qty
  100. * @throws LocalizedException
  101. */
  102. private function updateItemQuantity(Item $item, float $qty)
  103. {
  104. if ($qty > 0) {
  105. $item->setQty($qty);
  106. if ($item->getHasError()) {
  107. throw new LocalizedException(__($item->getMessage()));
  108. }
  109. }
  110. }
  111. /**
  112. * JSON response builder.
  113. *
  114. * @param string $error
  115. * @return void
  116. */
  117. private function jsonResponse(string $error = '')
  118. {
  119. $this->getResponse()->representJson(
  120. $this->json->serialize($this->getResponseData($error))
  121. );
  122. }
  123. /**
  124. * Returns response data.
  125. *
  126. * @param string $error
  127. * @return array
  128. */
  129. private function getResponseData(string $error = ''): array
  130. {
  131. $response = [
  132. 'success' => true,
  133. ];
  134. if (!empty($error)) {
  135. $response = [
  136. 'success' => false,
  137. 'error_message' => $error,
  138. ];
  139. }
  140. return $response;
  141. }
  142. }