9
9
use Magento \Catalog \Api \Data \ProductInterface ;
10
10
use Magento \Catalog \Api \ProductRepositoryInterface ;
11
11
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 ;
14
13
use Magento \Framework \Exception \InputException ;
15
14
use Magento \Framework \Exception \NoSuchEntityException ;
16
15
use Magento \Quote \Api \CartRepositoryInterface ;
22
21
use Magento \Sales \Model \Order \Item ;
23
22
use Magento \Sales \Model \OrderFactory ;
24
23
use Magento \Sales \Model \ResourceModel \Order \Item \Collection as ItemCollection ;
24
+ use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory as ProductCollectionFactory ;
25
25
26
26
/**
27
27
* Allows customer quickly to reorder previously added products and put them to the Cart
@@ -88,9 +88,9 @@ class Reorder
88
88
private $ customerCartProvider ;
89
89
90
90
/**
91
- * @var SearchCriteriaBuilder
91
+ * ProductCollectionFactory
92
92
*/
93
- private $ searchCriteriaBuilder ;
93
+ private $ productCollectionFactory ;
94
94
95
95
/**
96
96
* @param OrderFactory $orderFactory
@@ -99,7 +99,7 @@ class Reorder
99
99
* @param ProductRepositoryInterface $productRepository
100
100
* @param ReorderHelper $reorderHelper
101
101
* @param \Psr\Log\LoggerInterface $logger
102
- * @param SearchCriteriaBuilder $searchCriteriaBuilder = null
102
+ * @param ProductCollectionFactory $productCollectionFactory
103
103
*/
104
104
public function __construct (
105
105
OrderFactory $ orderFactory ,
@@ -108,16 +108,15 @@ public function __construct(
108
108
ProductRepositoryInterface $ productRepository ,
109
109
ReorderHelper $ reorderHelper ,
110
110
\Psr \Log \LoggerInterface $ logger ,
111
- SearchCriteriaBuilder $ searchCriteriaBuilder = null
111
+ ProductCollectionFactory $ productCollectionFactory
112
112
) {
113
113
$ this ->orderFactory = $ orderFactory ;
114
114
$ this ->cartRepository = $ cartRepository ;
115
115
$ this ->productRepository = $ productRepository ;
116
116
$ this ->reorderHelper = $ reorderHelper ;
117
117
$ this ->logger = $ logger ;
118
118
$ this ->customerCartProvider = $ customerCartProvider ;
119
- $ this ->searchCriteriaBuilder = $ searchCriteriaBuilder ?:
120
- ObjectManager::getInstance ()->get (SearchCriteriaBuilder::class);
119
+ $ this ->productCollectionFactory = $ productCollectionFactory ;
121
120
}
122
121
123
122
/**
@@ -148,7 +147,7 @@ public function execute(string $orderNumber, string $storeId): Data\ReorderOutpu
148
147
return $ this ->prepareOutput ($ cart );
149
148
}
150
149
151
- $ this ->addItemsToCart ($ cart , $ order ->getItemsCollection ());
150
+ $ this ->addItemsToCart ($ cart , $ order ->getItemsCollection (), $ storeId );
152
151
153
152
try {
154
153
$ this ->cartRepository ->save ($ cart );
@@ -167,9 +166,10 @@ public function execute(string $orderNumber, string $storeId): Data\ReorderOutpu
167
166
*
168
167
* @param Quote $cart
169
168
* @param ItemCollection $orderItems
169
+ * @param string $storeId
170
170
* @return void
171
171
*/
172
- private function addItemsToCart (Quote $ cart , ItemCollection $ orderItems ): void
172
+ private function addItemsToCart (Quote $ cart , ItemCollection $ orderItems, string $ storeId ): void
173
173
{
174
174
$ orderItemProductIds = [];
175
175
/** @var \Magento\Sales\Model\Order\Item[] $orderItemsByProductId */
@@ -183,13 +183,13 @@ private function addItemsToCart(Quote $cart, ItemCollection $orderItems): void
183
183
}
184
184
}
185
185
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 ();
190
188
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 ();
193
193
194
194
// compare founded products and throw an error if some product not exists
195
195
$ productsNotFound = array_diff ($ orderItemProductIds , array_keys ($ products ));
0 commit comments