Skip to content

Commit 551ddb8

Browse files
committed
MC-33400: [Improvement] Magento\Checkout\Model\Cart::addOrderItem
1 parent a4dfeba commit 551ddb8

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
use Magento\Catalog\Api\Data\ProductInterface;
1010
use Magento\Catalog\Api\ProductRepositoryInterface;
1111
use Magento\Catalog\Model\Product;
12-
use Magento\Framework\Api\SearchCriteriaBuilder;
13-
use Magento\Framework\App\ObjectManager;
12+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
1413
use Magento\Framework\Exception\InputException;
1514
use Magento\Framework\Exception\NoSuchEntityException;
1615
use Magento\Quote\Api\CartRepositoryInterface;
@@ -22,6 +21,7 @@
2221
use Magento\Sales\Model\Order\Item;
2322
use Magento\Sales\Model\OrderFactory;
2423
use Magento\Sales\Model\ResourceModel\Order\Item\Collection as ItemCollection;
24+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
2525

2626
/**
2727
* Allows customer quickly to reorder previously added products and put them to the Cart
@@ -88,9 +88,9 @@ class Reorder
8888
private $customerCartProvider;
8989

9090
/**
91-
* @var SearchCriteriaBuilder
91+
* ProductCollectionFactory
9292
*/
93-
private $searchCriteriaBuilder;
93+
private $productCollectionFactory;
9494

9595
/**
9696
* @param OrderFactory $orderFactory
@@ -99,7 +99,7 @@ class Reorder
9999
* @param ProductRepositoryInterface $productRepository
100100
* @param ReorderHelper $reorderHelper
101101
* @param \Psr\Log\LoggerInterface $logger
102-
* @param SearchCriteriaBuilder $searchCriteriaBuilder = null
102+
* @param ProductCollectionFactory $productCollectionFactory
103103
*/
104104
public function __construct(
105105
OrderFactory $orderFactory,
@@ -108,16 +108,15 @@ public function __construct(
108108
ProductRepositoryInterface $productRepository,
109109
ReorderHelper $reorderHelper,
110110
\Psr\Log\LoggerInterface $logger,
111-
SearchCriteriaBuilder $searchCriteriaBuilder = null
111+
ProductCollectionFactory $productCollectionFactory
112112
) {
113113
$this->orderFactory = $orderFactory;
114114
$this->cartRepository = $cartRepository;
115115
$this->productRepository = $productRepository;
116116
$this->reorderHelper = $reorderHelper;
117117
$this->logger = $logger;
118118
$this->customerCartProvider = $customerCartProvider;
119-
$this->searchCriteriaBuilder = $searchCriteriaBuilder ?:
120-
ObjectManager::getInstance()->get(SearchCriteriaBuilder::class);
119+
$this->productCollectionFactory = $productCollectionFactory;
121120
}
122121

123122
/**
@@ -148,7 +147,7 @@ public function execute(string $orderNumber, string $storeId): Data\ReorderOutpu
148147
return $this->prepareOutput($cart);
149148
}
150149

151-
$this->addItemsToCart($cart, $order->getItemsCollection());
150+
$this->addItemsToCart($cart, $order->getItemsCollection(), $storeId);
152151

153152
try {
154153
$this->cartRepository->save($cart);
@@ -167,9 +166,10 @@ public function execute(string $orderNumber, string $storeId): Data\ReorderOutpu
167166
*
168167
* @param Quote $cart
169168
* @param ItemCollection $orderItems
169+
* @param string $storeId
170170
* @return void
171171
*/
172-
private function addItemsToCart(Quote $cart, ItemCollection $orderItems): void
172+
private function addItemsToCart(Quote $cart, ItemCollection $orderItems, string $storeId): void
173173
{
174174
$orderItemProductIds = [];
175175
/** @var \Magento\Sales\Model\Order\Item[] $orderItemsByProductId */
@@ -183,13 +183,13 @@ private function addItemsToCart(Quote $cart, ItemCollection $orderItems): void
183183
}
184184
}
185185

186-
/** @var \Magento\Framework\Api\SearchCriteria $criteria */
187-
$criteria = $this->searchCriteriaBuilder
188-
->addFilter('entity_id', $orderItemProductIds, 'in')
189-
->create();
186+
/** @var Collection $collection */
187+
$collection = $this->productCollectionFactory->create();
190188

191-
/** @var \Magento\Catalog\Api\Data\ProductInterface $products */
192-
$products = $this->productRepository->getList($criteria)->getItems();
189+
$collection->setStore($storeId);
190+
$collection->addIdFilter($orderItemProductIds);
191+
$collection->addStoreFilter();
192+
$products = $collection->getItems();
193193

194194
// compare founded products and throw an error if some product not exists
195195
$productsNotFound = array_diff($orderItemProductIds, array_keys($products));

0 commit comments

Comments
 (0)