6 declare(strict_types=1);
20 use Psr\Log\LoggerInterface;
27 private $checkoutSession;
61 MultishippingHelper $helper,
65 $this->checkoutSession = $checkoutSession;
84 $shippingInfo = $this->
getRequest()->getPost(
'ship');
85 if (!\is_array($shippingInfo)) {
87 __(
'We are unable to process your request. Please, try again later.')
91 $itemsInfo = $this->collectItemsInfo($shippingInfo);
92 $totalQuantity = array_sum($itemsInfo);
94 $maxQuantity = $this->helper->getMaximumQty();
95 if ($totalQuantity > $maxQuantity) {
97 __(
'Maximum qty allowed for Shipping to multiple addresses is %1', $maxQuantity)
101 $quote = $this->checkoutSession->getQuote();
103 if (isset($itemsInfo[
$item->getId()])) {
104 $this->updateItemQuantity(
$item, $itemsInfo[
$item->getId()]);
108 if (
$quote->getHasError()) {
112 $this->jsonResponse();
114 $this->jsonResponse($e->getMessage());
116 $this->logger->critical($e->getMessage());
117 $this->jsonResponse(
'We are unable to process your request. Please, try again later.');
128 private function updateItemQuantity(
Item $item,
float $quantity)
131 $item->setQty($quantity);
132 if (
$item->getHasError()) {
144 private function collectItemsInfo(array $shippingInfo): array
147 foreach ($shippingInfo as $itemData) {
148 if (!\is_array($itemData)) {
168 private function jsonResponse(
string $error =
'')
171 $this->json->serialize($this->getResponseData($error))
181 private function getResponseData(
string $error =
''): array
187 if (!empty($error)) {
190 'error_message' => $error,
__construct(Context $context, CustomerSession $customerSession, CustomerRepositoryInterface $customerRepository, AccountManagementInterface $accountManagement, CheckoutSession $checkoutSession, MultishippingHelper $helper, Json $json, LoggerInterface $logger)