Skip to content

Commit 6523cc0

Browse files
committed
MC-33400: [Improvement] Magento\Checkout\Model\Cart::addOrderItem
1 parent 13b80b4 commit 6523cc0

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

app/code/Magento/Sales/Model/Reorder/Reorder.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,16 @@ public function execute(string $orderNumber, string $storeId): Data\ReorderOutpu
148148
return $this->prepareOutput($cart);
149149
}
150150

151-
$cartWithItems = $this->addItemsToCart($cart, $order->getItemsCollection());
151+
$this->addItemsToCart($cart, $order->getItemsCollection());
152152

153153
try {
154-
$this->cartRepository->save($cartWithItems);
154+
$this->cartRepository->save($cart);
155155
} catch (\Magento\Framework\Exception\LocalizedException $e) {
156156
// handle exception from \Magento\Quote\Model\QuoteRepository\SaveHandler::save
157157
$this->addError($e->getMessage());
158158
}
159159

160-
$savedCart = $this->cartRepository->get($cartWithItems->getId());
160+
$savedCart = $this->cartRepository->get($cart->getId());
161161

162162
return $this->prepareOutput($savedCart);
163163
}
@@ -167,9 +167,9 @@ public function execute(string $orderNumber, string $storeId): Data\ReorderOutpu
167167
*
168168
* @param Quote $cart
169169
* @param ItemCollection $orderItems
170-
* @return Quote
170+
* @return void
171171
*/
172-
private function addItemsToCart(Quote $cart, ItemCollection $orderItems): Quote
172+
private function addItemsToCart(Quote $cart, ItemCollection $orderItems): void
173173
{
174174
$orderItemProductIds = [];
175175
/** @var \Magento\Sales\Model\Order\Item[] $orderItemsByProductId */
@@ -179,10 +179,11 @@ private function addItemsToCart(Quote $cart, ItemCollection $orderItems): Quote
179179
foreach ($orderItems as $item) {
180180
if ($item->getParentItem() === null) {
181181
$orderItemProductIds[] = $item->getProductId();
182-
$orderItemsByProductId[$item->getProductId()] = $item;
182+
$orderItemsByProductId[$item->getProductId()][$item->getId()] = $item;
183183
}
184184
}
185185

186+
/** @var \Magento\Framework\Api\SearchCriteria $criteria */
186187
$criteria = $this->searchCriteriaBuilder
187188
->addFilter('entity_id', $orderItemProductIds, 'in')
188189
->create();
@@ -204,11 +205,14 @@ private function addItemsToCart(Quote $cart, ItemCollection $orderItems): Quote
204205

205206
/** @var ProductInterface $product */
206207
foreach ($products as $product) {
207-
$orderItem = $orderItemsByProductId[$product->getId()];
208-
$this->addItemToCart($orderItem, $cart, $product);
208+
if (!isset($orderItemsByProductId[$product->getId()])) {
209+
continue;
210+
}
211+
$orderItems = $orderItemsByProductId[$product->getId()];
212+
foreach ($orderItems as $orderItem) {
213+
$this->addItemToCart($orderItem, $cart, $product);
214+
}
209215
}
210-
211-
return $cart;
212216
}
213217

214218
/**
@@ -217,9 +221,9 @@ private function addItemsToCart(Quote $cart, ItemCollection $orderItems): Quote
217221
* @param OrderItemInterface $orderItem
218222
* @param Quote $cart
219223
* @param ProductInterface $product
220-
* @return Quote
224+
* @return void
221225
*/
222-
private function addItemToCart(OrderItemInterface $orderItem, Quote $cart, ProductInterface $product): Quote
226+
private function addItemToCart(OrderItemInterface $orderItem, Quote $cart, ProductInterface $product): void
223227
{
224228
$info = $orderItem->getProductOptionByCode('info_buyRequest');
225229
$info = new \Magento\Framework\DataObject($info);
@@ -242,8 +246,6 @@ private function addItemToCart(OrderItemInterface $orderItem, Quote $cart, Produ
242246
$this->addError($this->getCartItemErrorMessage($orderItem, $product, $error));
243247
}
244248
}
245-
246-
return $cart;
247249
}
248250

249251
/**

0 commit comments

Comments
 (0)