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