8
8
9
9
use Magento \Catalog \Api \Data \ProductInterface ;
10
10
use Magento \Catalog \Model \Product ;
11
- use Magento \Catalog \Model \ResourceModel \Product \Collection ;
12
11
use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory as ProductCollectionFactory ;
13
12
use Magento \Framework \DataObject ;
13
+ use Magento \Framework \Exception \AlreadyExistsException ;
14
14
use Magento \Framework \Exception \InputException ;
15
+ use Magento \Framework \Exception \LocalizedException ;
15
16
use Magento \Framework \Exception \NoSuchEntityException ;
16
17
use Magento \Quote \Api \CartRepositoryInterface ;
17
18
use Magento \Quote \Api \Data \CartInterface ;
24
25
use Magento \Sales \Model \OrderFactory ;
25
26
use Magento \Framework \App \ObjectManager ;
26
27
use Magento \Store \Model \StoreManagerInterface ;
28
+ use Magento \Framework \Exception \CouldNotSaveException ;
27
29
use Magento \Sales \Model \ResourceModel \Order \Item \Collection as ItemCollection ;
30
+ use Magento \Customer \Model \Session as CustomerSession ;
28
31
use Psr \Log \LoggerInterface ;
29
32
30
33
/**
@@ -116,6 +119,11 @@ class Reorder
116
119
*/
117
120
private bool $ addToCartInvalidProduct ;
118
121
122
+ /**
123
+ * @var CustomerSession
124
+ */
125
+ private $ customerSession ;
126
+
119
127
/**
120
128
* @param OrderFactory $orderFactory
121
129
* @param CustomerCartResolver $customerCartProvider
@@ -127,7 +135,7 @@ class Reorder
127
135
* @param OrderInfoBuyRequestGetter $orderInfoBuyRequestGetter
128
136
* @param StoreManagerInterface|null $storeManager
129
137
* @param bool $addToCartInvalidProduct
130
- *
138
+ * @param CustomerSession|null $customerSession
131
139
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
132
140
*/
133
141
public function __construct (
@@ -140,7 +148,8 @@ public function __construct(
140
148
ProductCollectionFactory $ productCollectionFactory ,
141
149
OrderInfoBuyRequestGetter $ orderInfoBuyRequestGetter ,
142
150
?StoreManagerInterface $ storeManager = null ,
143
- bool $ addToCartInvalidProduct = false
151
+ bool $ addToCartInvalidProduct = false ,
152
+ ?CustomerSession $ customerSession = null
144
153
) {
145
154
$ this ->orderFactory = $ orderFactory ;
146
155
$ this ->cartRepository = $ cartRepository ;
@@ -153,6 +162,8 @@ public function __construct(
153
162
$ this ->storeManager = $ storeManager
154
163
?: ObjectManager::getInstance ()->get (StoreManagerInterface::class);
155
164
$ this ->addToCartInvalidProduct = $ addToCartInvalidProduct ;
165
+ $ this ->customerSession = $ customerSession
166
+ ?: ObjectManager::getInstance ()->get (CustomerSession::class);
156
167
}
157
168
158
169
/**
@@ -163,7 +174,9 @@ public function __construct(
163
174
* @return Data\ReorderOutput
164
175
* @throws InputException Order is not found
165
176
* @throws NoSuchEntityException The specified customer does not exist.
166
- * @throws \Magento\Framework\Exception\CouldNotSaveException Could not create customer Cart
177
+ * @throws CouldNotSaveException
178
+ * @throws AlreadyExistsException
179
+ * @throws LocalizedException
167
180
*/
168
181
public function execute (string $ orderNumber , string $ storeId ): Data \ReorderOutput
169
182
{
@@ -174,10 +187,10 @@ public function execute(string $orderNumber, string $storeId): Data\ReorderOutpu
174
187
__ ('Cannot find order number "%1" in store "%2" ' , $ orderNumber , $ storeId )
175
188
);
176
189
}
177
- $ customerId = (int )$ order ->getCustomerId ();
190
+ $ customerId = (int ) $ order ->getCustomerId ();
178
191
$ this ->errors = [];
179
192
180
- $ cart = $ customerId === 0
193
+ $ cart = $ this -> isCustomerReorderAsGuest ( $ customerId)
181
194
? $ this ->guestCartResolver ->resolve ()
182
195
: $ this ->customerCartProvider ->resolve ($ customerId );
183
196
if (!$ this ->reorderHelper ->isAllowed ($ order ->getStore ())) {
@@ -190,7 +203,7 @@ public function execute(string $orderNumber, string $storeId): Data\ReorderOutpu
190
203
191
204
try {
192
205
$ this ->cartRepository ->save ($ cart );
193
- } catch (\ Magento \ Framework \ Exception \ LocalizedException $ e ) {
206
+ } catch (LocalizedException $ e ) {
194
207
// handle exception from \Magento\Quote\Model\QuoteRepository\SaveHandler::save
195
208
$ this ->addError ($ e ->getMessage ());
196
209
}
@@ -207,14 +220,15 @@ public function execute(string $orderNumber, string $storeId): Data\ReorderOutpu
207
220
* @param ItemCollection $orderItems
208
221
* @param string $storeId
209
222
* @return void
223
+ * @throws LocalizedException
210
224
*/
211
225
private function addItemsToCart (Quote $ cart , ItemCollection $ orderItems , string $ storeId ): void
212
226
{
213
227
$ orderItemProductIds = [];
214
- /** @var \Magento\Sales\Model\Order\ Item[] $orderItemsByProductId */
228
+ /** @var Item[] $orderItemsByProductId */
215
229
$ orderItemsByProductId = [];
216
230
217
- /** @var \Magento\Sales\Model\Order\ Item $item */
231
+ /** @var Item $item */
218
232
foreach ($ orderItems as $ item ) {
219
233
if ($ item ->getParentItem () === null ) {
220
234
$ orderItemProductIds [] = $ item ->getProductId ();
@@ -228,7 +242,7 @@ private function addItemsToCart(Quote $cart, ItemCollection $orderItems, string
228
242
$ productsNotFound = array_diff ($ orderItemProductIds , array_keys ($ products ));
229
243
if (!empty ($ productsNotFound )) {
230
244
foreach ($ productsNotFound as $ productId ) {
231
- /** @var \Magento\Sales\Model\Order\ Item $orderItemProductNotFound */
245
+ /** @var Item $orderItemProductNotFound */
232
246
$ this ->addError (
233
247
(string )__ ('Could not find a product with ID "%1" ' , $ productId ),
234
248
self ::ERROR_PRODUCT_NOT_FOUND
@@ -253,11 +267,10 @@ private function addItemsToCart(Quote $cart, ItemCollection $orderItems, string
253
267
* @param string $storeId
254
268
* @param int[] $orderItemProductIds
255
269
* @return Product[]
256
- * @throws \Magento\Framework\Exception\ LocalizedException
270
+ * @throws LocalizedException
257
271
*/
258
272
private function getOrderProducts (string $ storeId , array $ orderItemProductIds ): array
259
273
{
260
- /** @var Collection $collection */
261
274
$ collection = $ this ->productCollectionFactory ->create ();
262
275
$ collection ->setFlag ('has_stock_status_filter ' , true );
263
276
$ collection ->setStore ($ storeId )
@@ -288,7 +301,7 @@ private function addItemToCart(OrderItemInterface $orderItem, Quote $cart, Produ
288
301
try {
289
302
$ infoBuyRequest ->setAddToCartInvalidProduct ($ this ->addToCartInvalidProduct );
290
303
$ addProductResult = $ cart ->addProduct ($ product , $ infoBuyRequest );
291
- } catch (\ Magento \ Framework \ Exception \ LocalizedException $ e ) {
304
+ } catch (LocalizedException $ e ) {
292
305
$ this ->addError ($ this ->getCartItemErrorMessage ($ orderItem , $ product , $ e ->getMessage ()));
293
306
} catch (\Throwable $ e ) {
294
307
$ this ->logger ->critical ($ e );
@@ -391,4 +404,15 @@ private function getCartItemErrorMessage(Item $item, Product $product, string $m
391
404
? __ ('Could not add the product with SKU "%1" to the shopping cart: %2 ' , $ sku , $ message )
392
405
: __ ('Could not add the product with SKU "%1" to the shopping cart ' , $ sku ));
393
406
}
407
+
408
+ /**
409
+ * Check customer re-order as guest customer
410
+ *
411
+ * @param int $customerId
412
+ * @return bool
413
+ */
414
+ private function isCustomerReorderAsGuest (int $ customerId ): bool
415
+ {
416
+ return $ customerId === 0 || !$ this ->customerSession ->isLoggedIn ();
417
+ }
394
418
}
0 commit comments