Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CheckItems.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
18 use Magento\Multishipping\Helper\Data as MultishippingHelper;
20 use Psr\Log\LoggerInterface;
21 
22 class CheckItems extends Checkout
23 {
27  private $checkoutSession;
28 
32  private $helper;
33 
37  private $json;
38 
42  private $logger;
43 
55  public function __construct(
56  Context $context,
57  CustomerSession $customerSession,
60  CheckoutSession $checkoutSession,
61  MultishippingHelper $helper,
62  Json $json,
63  LoggerInterface $logger
64  ) {
65  $this->checkoutSession = $checkoutSession;
66  $this->helper = $helper;
67  $this->json = $json;
68  $this->logger = $logger;
69 
70  parent::__construct(
71  $context,
72  $customerSession,
75  );
76  }
77 
81  public function execute()
82  {
83  try {
84  $shippingInfo = $this->getRequest()->getPost('ship');
85  if (!\is_array($shippingInfo)) {
86  throw new LocalizedException(
87  __('We are unable to process your request. Please, try again later.')
88  );
89  }
90 
91  $itemsInfo = $this->collectItemsInfo($shippingInfo);
92  $totalQuantity = array_sum($itemsInfo);
93 
94  $maxQuantity = $this->helper->getMaximumQty();
95  if ($totalQuantity > $maxQuantity) {
96  throw new LocalizedException(
97  __('Maximum qty allowed for Shipping to multiple addresses is %1', $maxQuantity)
98  );
99  }
100 
101  $quote = $this->checkoutSession->getQuote();
102  foreach ($quote->getAllItems() as $item) {
103  if (isset($itemsInfo[$item->getId()])) {
104  $this->updateItemQuantity($item, $itemsInfo[$item->getId()]);
105  }
106  }
107 
108  if ($quote->getHasError()) {
109  throw new LocalizedException(__($quote->getMessage()));
110  }
111 
112  $this->jsonResponse();
113  } catch (LocalizedException $e) {
114  $this->jsonResponse($e->getMessage());
115  } catch (\Exception $e) {
116  $this->logger->critical($e->getMessage());
117  $this->jsonResponse('We are unable to process your request. Please, try again later.');
118  }
119  }
120 
128  private function updateItemQuantity(Item $item, float $quantity)
129  {
130  if ($quantity > 0) {
131  $item->setQty($quantity);
132  if ($item->getHasError()) {
133  throw new LocalizedException(__($item->getMessage()));
134  }
135  }
136  }
137 
144  private function collectItemsInfo(array $shippingInfo): array
145  {
146  $itemsInfo = [];
147  foreach ($shippingInfo as $itemData) {
148  if (!\is_array($itemData)) {
149  continue;
150  }
151  foreach ($itemData as $quoteItemId => $data) {
152  if (!isset($itemsInfo[$quoteItemId])) {
153  $itemsInfo[$quoteItemId] = 0;
154  }
155  $itemsInfo[$quoteItemId] += (double)$data['qty'];
156  }
157  }
158 
159  return $itemsInfo;
160  }
161 
168  private function jsonResponse(string $error = '')
169  {
170  $this->getResponse()->representJson(
171  $this->json->serialize($this->getResponseData($error))
172  );
173  }
174 
181  private function getResponseData(string $error = ''): array
182  {
183  $response = [
184  'success' => true,
185  ];
186 
187  if (!empty($error)) {
188  $response = [
189  'success' => false,
190  'error_message' => $error,
191  ];
192  }
193 
194  return $response;
195  }
196 }
$response
Definition: 404.php:11
$helper
Definition: iframe.phtml:13
$quote
__()
Definition: __.php:13
$logger
$quoteItemId
Definition: cart.php:17
__construct(Context $context, CustomerSession $customerSession, CustomerRepositoryInterface $customerRepository, AccountManagementInterface $accountManagement, CheckoutSession $checkoutSession, MultishippingHelper $helper, Json $json, LoggerInterface $logger)
Definition: CheckItems.php:55