CheckItems.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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\Multishipping\Controller\Checkout;
  8. use Magento\Customer\Model\Session as CustomerSession;
  9. use Magento\Framework\App\Action\Context;
  10. use Magento\Framework\Exception\LocalizedException;
  11. use Magento\Framework\Serialize\Serializer\Json;
  12. use Magento\Checkout\Model\Session as CheckoutSession;
  13. use Magento\Customer\Api\AccountManagementInterface;
  14. use Magento\Customer\Api\CustomerRepositoryInterface;
  15. use Magento\Multishipping\Controller\Checkout;
  16. use Magento\Multishipping\Helper\Data as MultishippingHelper;
  17. use Magento\Quote\Model\Quote\Item;
  18. use Psr\Log\LoggerInterface;
  19. class CheckItems extends Checkout
  20. {
  21. /**
  22. * @var CheckoutSession
  23. */
  24. private $checkoutSession;
  25. /**
  26. * @var MultishippingHelper
  27. */
  28. private $helper;
  29. /**
  30. * @var Json
  31. */
  32. private $json;
  33. /**
  34. * @var LoggerInterface
  35. */
  36. private $logger;
  37. /**
  38. * @param Context $context,
  39. * @param CustomerSession $customerSession
  40. * @param CustomerRepositoryInterface $customerRepository
  41. * @param AccountManagementInterface $accountManagement
  42. * @param CheckoutSession $checkoutSession
  43. * @param MultishippingHelper $helper
  44. * @param Json $json
  45. * @param LoggerInterface $logger
  46. */
  47. public function __construct(
  48. Context $context,
  49. CustomerSession $customerSession,
  50. CustomerRepositoryInterface $customerRepository,
  51. AccountManagementInterface $accountManagement,
  52. CheckoutSession $checkoutSession,
  53. MultishippingHelper $helper,
  54. Json $json,
  55. LoggerInterface $logger
  56. ) {
  57. $this->checkoutSession = $checkoutSession;
  58. $this->helper = $helper;
  59. $this->json = $json;
  60. $this->logger = $logger;
  61. parent::__construct(
  62. $context,
  63. $customerSession,
  64. $customerRepository,
  65. $accountManagement
  66. );
  67. }
  68. /**
  69. * @return void
  70. */
  71. public function execute()
  72. {
  73. try {
  74. $shippingInfo = $this->getRequest()->getPost('ship');
  75. if (!\is_array($shippingInfo)) {
  76. throw new LocalizedException(
  77. __('We are unable to process your request. Please, try again later.')
  78. );
  79. }
  80. $itemsInfo = $this->collectItemsInfo($shippingInfo);
  81. $totalQuantity = array_sum($itemsInfo);
  82. $maxQuantity = $this->helper->getMaximumQty();
  83. if ($totalQuantity > $maxQuantity) {
  84. throw new LocalizedException(
  85. __('Maximum qty allowed for Shipping to multiple addresses is %1', $maxQuantity)
  86. );
  87. }
  88. $quote = $this->checkoutSession->getQuote();
  89. foreach ($quote->getAllItems() as $item) {
  90. if (isset($itemsInfo[$item->getId()])) {
  91. $this->updateItemQuantity($item, $itemsInfo[$item->getId()]);
  92. }
  93. }
  94. if ($quote->getHasError()) {
  95. throw new LocalizedException(__($quote->getMessage()));
  96. }
  97. $this->jsonResponse();
  98. } catch (LocalizedException $e) {
  99. $this->jsonResponse($e->getMessage());
  100. } catch (\Exception $e) {
  101. $this->logger->critical($e->getMessage());
  102. $this->jsonResponse('We are unable to process your request. Please, try again later.');
  103. }
  104. }
  105. /**
  106. * Updates quote item quantity.
  107. *
  108. * @param Item $item
  109. * @param float $quantity
  110. * @throws LocalizedException
  111. */
  112. private function updateItemQuantity(Item $item, float $quantity)
  113. {
  114. if ($quantity > 0) {
  115. $item->setQty($quantity);
  116. if ($item->getHasError()) {
  117. throw new LocalizedException(__($item->getMessage()));
  118. }
  119. }
  120. }
  121. /**
  122. * Group posted items.
  123. *
  124. * @param array $shippingInfo
  125. * @return array
  126. */
  127. private function collectItemsInfo(array $shippingInfo): array
  128. {
  129. $itemsInfo = [];
  130. foreach ($shippingInfo as $itemData) {
  131. if (!\is_array($itemData)) {
  132. continue;
  133. }
  134. foreach ($itemData as $quoteItemId => $data) {
  135. if (!isset($itemsInfo[$quoteItemId])) {
  136. $itemsInfo[$quoteItemId] = 0;
  137. }
  138. $itemsInfo[$quoteItemId] += (double)$data['qty'];
  139. }
  140. }
  141. return $itemsInfo;
  142. }
  143. /**
  144. * JSON response builder.
  145. *
  146. * @param string $error
  147. * @return void
  148. */
  149. private function jsonResponse(string $error = '')
  150. {
  151. $this->getResponse()->representJson(
  152. $this->json->serialize($this->getResponseData($error))
  153. );
  154. }
  155. /**
  156. * Returns response data.
  157. *
  158. * @param string $error
  159. * @return array
  160. */
  161. private function getResponseData(string $error = ''): array
  162. {
  163. $response = [
  164. 'success' => true,
  165. ];
  166. if (!empty($error)) {
  167. $response = [
  168. 'success' => false,
  169. 'error_message' => $error,
  170. ];
  171. }
  172. return $response;
  173. }
  174. }