|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Multishipping\Plugin; |
| 9 | + |
| 10 | +use Magento\Framework\Api\SearchResultsInterface; |
| 11 | +use Magento\Quote\Api\CartRepositoryInterface; |
| 12 | +use Magento\Quote\Api\Data\CartInterface; |
| 13 | +use Magento\Quote\Model\Quote; |
| 14 | +use Magento\Quote\Model\Quote\ShippingAssignment\ShippingProcessor; |
| 15 | +use Magento\Quote\Model\ShippingAssignmentFactory; |
| 16 | + |
| 17 | +/** |
| 18 | + * Plugin for multishipping quote processing. |
| 19 | + */ |
| 20 | +class MultishippingQuoteRepository |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var ShippingAssignmentFactory |
| 24 | + */ |
| 25 | + private $shippingAssignmentFactory; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var ShippingProcessor |
| 29 | + */ |
| 30 | + private $shippingProcessor; |
| 31 | + |
| 32 | + /** |
| 33 | + * @param ShippingAssignmentFactory $shippingAssignmentFactory |
| 34 | + * @param ShippingProcessor $shippingProcessor |
| 35 | + */ |
| 36 | + public function __construct( |
| 37 | + ShippingAssignmentFactory $shippingAssignmentFactory, |
| 38 | + ShippingProcessor $shippingProcessor |
| 39 | + ) { |
| 40 | + $this->shippingAssignmentFactory = $shippingAssignmentFactory; |
| 41 | + $this->shippingProcessor = $shippingProcessor; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Process multishipping quote for get. |
| 46 | + * |
| 47 | + * @param CartRepositoryInterface $subject |
| 48 | + * @param CartInterface $result |
| 49 | + * @return CartInterface |
| 50 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 51 | + */ |
| 52 | + public function afterGet( |
| 53 | + CartRepositoryInterface $subject, |
| 54 | + CartInterface $result |
| 55 | + ) { |
| 56 | + return $this->processQuote($result); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Process multishipping quote for get list. |
| 61 | + * |
| 62 | + * @param CartRepositoryInterface $subject |
| 63 | + * @param SearchResultsInterface $result |
| 64 | + * |
| 65 | + * @return SearchResultsInterface |
| 66 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 67 | + */ |
| 68 | + public function afterGetList( |
| 69 | + CartRepositoryInterface $subject, |
| 70 | + SearchResultsInterface $result |
| 71 | + ) { |
| 72 | + $items = []; |
| 73 | + foreach ($result->getItems() as $item) { |
| 74 | + $items[] = $this->processQuote($item); |
| 75 | + } |
| 76 | + $result->setItems($items); |
| 77 | + |
| 78 | + return $result; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Remove shipping assignments for multishipping quote. |
| 83 | + * |
| 84 | + * @param CartRepositoryInterface $subject |
| 85 | + * @param CartInterface $quote |
| 86 | + * @return array |
| 87 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 88 | + */ |
| 89 | + public function beforeSave(CartRepositoryInterface $subject, CartInterface $quote) |
| 90 | + { |
| 91 | + $extensionAttributes = $quote->getExtensionAttributes(); |
| 92 | + if ($quote->getIsMultiShipping() && $extensionAttributes && $extensionAttributes->getShippingAssignments()) { |
| 93 | + $quote->getExtensionAttributes()->setShippingAssignments([]); |
| 94 | + } |
| 95 | + |
| 96 | + return [$quote]; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Set shipping assignments for multishipping quote according to customer selection. |
| 101 | + * |
| 102 | + * @param CartInterface $quote |
| 103 | + * @return CartInterface |
| 104 | + */ |
| 105 | + private function processQuote(CartInterface $quote): CartInterface |
| 106 | + { |
| 107 | + if (!$quote->getIsMultiShipping() || !$quote instanceof Quote) { |
| 108 | + return $quote; |
| 109 | + } |
| 110 | + |
| 111 | + if ($quote->getExtensionAttributes() && $quote->getExtensionAttributes()->getShippingAssignments()) { |
| 112 | + $shippingAssignments = []; |
| 113 | + $addresses = $quote->getAllAddresses(); |
| 114 | + |
| 115 | + foreach ($addresses as $address) { |
| 116 | + $quoteItems = $this->getQuoteItems($quote, $address); |
| 117 | + if (!empty($quoteItems)) { |
| 118 | + $shippingAssignment = $this->shippingAssignmentFactory->create(); |
| 119 | + $shippingAssignment->setItems($quoteItems); |
| 120 | + $shippingAssignment->setShipping($this->shippingProcessor->create($address)); |
| 121 | + $shippingAssignments[] = $shippingAssignment; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + if (!empty($shippingAssignments)) { |
| 126 | + $quote->getExtensionAttributes()->setShippingAssignments($shippingAssignments); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return $quote; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Returns quote items assigned to address. |
| 135 | + * |
| 136 | + * @param Quote $quote |
| 137 | + * @param Quote\Address $address |
| 138 | + * @return Quote\Item[] |
| 139 | + */ |
| 140 | + private function getQuoteItems(Quote $quote, Quote\Address $address): array |
| 141 | + { |
| 142 | + $quoteItems = []; |
| 143 | + foreach ($address->getItemsCollection() as $addressItem) { |
| 144 | + $quoteItem = $quote->getItemById($addressItem->getQuoteItemId()); |
| 145 | + if ($quoteItem) { |
| 146 | + $multishippingQuoteItem = clone $quoteItem; |
| 147 | + $qty = $addressItem->getQty(); |
| 148 | + $sku = $multishippingQuoteItem->getSku(); |
| 149 | + if (isset($quoteItems[$sku])) { |
| 150 | + $qty += $quoteItems[$sku]->getQty(); |
| 151 | + } |
| 152 | + $multishippingQuoteItem->setQty($qty); |
| 153 | + $quoteItems[$sku] = $multishippingQuoteItem; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + return array_values($quoteItems); |
| 158 | + } |
| 159 | +} |
0 commit comments