88
99use Magento \Catalog \Api \Data \ProductInterface ;
1010use Magento \Catalog \Model \Product ;
11- use Magento \Catalog \Model \ResourceModel \Product \Collection ;
1211use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory as ProductCollectionFactory ;
1312use Magento \Framework \DataObject ;
13+ use Magento \Framework \Exception \AlreadyExistsException ;
1414use Magento \Framework \Exception \InputException ;
15+ use Magento \Framework \Exception \LocalizedException ;
1516use Magento \Framework \Exception \NoSuchEntityException ;
1617use Magento \Quote \Api \CartRepositoryInterface ;
1718use Magento \Quote \Api \Data \CartInterface ;
2425use Magento \Sales \Model \OrderFactory ;
2526use Magento \Framework \App \ObjectManager ;
2627use Magento \Store \Model \StoreManagerInterface ;
28+ use Magento \Framework \Exception \CouldNotSaveException ;
2729use Magento \Sales \Model \ResourceModel \Order \Item \Collection as ItemCollection ;
30+ use Magento \Customer \Model \Session as CustomerSession ;
2831use Psr \Log \LoggerInterface ;
2932
3033/**
@@ -116,6 +119,11 @@ class Reorder
116119 */
117120 private bool $ addToCartInvalidProduct ;
118121
122+ /**
123+ * @var CustomerSession
124+ */
125+ private $ customerSession ;
126+
119127 /**
120128 * @param OrderFactory $orderFactory
121129 * @param CustomerCartResolver $customerCartProvider
@@ -127,7 +135,7 @@ class Reorder
127135 * @param OrderInfoBuyRequestGetter $orderInfoBuyRequestGetter
128136 * @param StoreManagerInterface|null $storeManager
129137 * @param bool $addToCartInvalidProduct
130- *
138+ * @param CustomerSession|null $customerSession
131139 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
132140 */
133141 public function __construct (
@@ -140,7 +148,8 @@ public function __construct(
140148 ProductCollectionFactory $ productCollectionFactory ,
141149 OrderInfoBuyRequestGetter $ orderInfoBuyRequestGetter ,
142150 ?StoreManagerInterface $ storeManager = null ,
143- bool $ addToCartInvalidProduct = false
151+ bool $ addToCartInvalidProduct = false ,
152+ ?CustomerSession $ customerSession = null
144153 ) {
145154 $ this ->orderFactory = $ orderFactory ;
146155 $ this ->cartRepository = $ cartRepository ;
@@ -153,6 +162,8 @@ public function __construct(
153162 $ this ->storeManager = $ storeManager
154163 ?: ObjectManager::getInstance ()->get (StoreManagerInterface::class);
155164 $ this ->addToCartInvalidProduct = $ addToCartInvalidProduct ;
165+ $ this ->customerSession = $ customerSession
166+ ?: ObjectManager::getInstance ()->get (CustomerSession::class);
156167 }
157168
158169 /**
@@ -163,7 +174,9 @@ public function __construct(
163174 * @return Data\ReorderOutput
164175 * @throws InputException Order is not found
165176 * @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
167180 */
168181 public function execute (string $ orderNumber , string $ storeId ): Data \ReorderOutput
169182 {
@@ -174,10 +187,10 @@ public function execute(string $orderNumber, string $storeId): Data\ReorderOutpu
174187 __ ('Cannot find order number "%1" in store "%2" ' , $ orderNumber , $ storeId )
175188 );
176189 }
177- $ customerId = (int )$ order ->getCustomerId ();
190+ $ customerId = (int ) $ order ->getCustomerId ();
178191 $ this ->errors = [];
179192
180- $ cart = $ customerId === 0
193+ $ cart = $ this -> isCustomerReorderAsGuest ( $ customerId)
181194 ? $ this ->guestCartResolver ->resolve ()
182195 : $ this ->customerCartProvider ->resolve ($ customerId );
183196 if (!$ this ->reorderHelper ->isAllowed ($ order ->getStore ())) {
@@ -190,7 +203,7 @@ public function execute(string $orderNumber, string $storeId): Data\ReorderOutpu
190203
191204 try {
192205 $ this ->cartRepository ->save ($ cart );
193- } catch (\ Magento \ Framework \ Exception \ LocalizedException $ e ) {
206+ } catch (LocalizedException $ e ) {
194207 // handle exception from \Magento\Quote\Model\QuoteRepository\SaveHandler::save
195208 $ this ->addError ($ e ->getMessage ());
196209 }
@@ -207,14 +220,15 @@ public function execute(string $orderNumber, string $storeId): Data\ReorderOutpu
207220 * @param ItemCollection $orderItems
208221 * @param string $storeId
209222 * @return void
223+ * @throws LocalizedException
210224 */
211225 private function addItemsToCart (Quote $ cart , ItemCollection $ orderItems , string $ storeId ): void
212226 {
213227 $ orderItemProductIds = [];
214- /** @var \Magento\Sales\Model\Order\ Item[] $orderItemsByProductId */
228+ /** @var Item[] $orderItemsByProductId */
215229 $ orderItemsByProductId = [];
216230
217- /** @var \Magento\Sales\Model\Order\ Item $item */
231+ /** @var Item $item */
218232 foreach ($ orderItems as $ item ) {
219233 if ($ item ->getParentItem () === null ) {
220234 $ orderItemProductIds [] = $ item ->getProductId ();
@@ -228,7 +242,7 @@ private function addItemsToCart(Quote $cart, ItemCollection $orderItems, string
228242 $ productsNotFound = array_diff ($ orderItemProductIds , array_keys ($ products ));
229243 if (!empty ($ productsNotFound )) {
230244 foreach ($ productsNotFound as $ productId ) {
231- /** @var \Magento\Sales\Model\Order\ Item $orderItemProductNotFound */
245+ /** @var Item $orderItemProductNotFound */
232246 $ this ->addError (
233247 (string )__ ('Could not find a product with ID "%1" ' , $ productId ),
234248 self ::ERROR_PRODUCT_NOT_FOUND
@@ -253,11 +267,10 @@ private function addItemsToCart(Quote $cart, ItemCollection $orderItems, string
253267 * @param string $storeId
254268 * @param int[] $orderItemProductIds
255269 * @return Product[]
256- * @throws \Magento\Framework\Exception\ LocalizedException
270+ * @throws LocalizedException
257271 */
258272 private function getOrderProducts (string $ storeId , array $ orderItemProductIds ): array
259273 {
260- /** @var Collection $collection */
261274 $ collection = $ this ->productCollectionFactory ->create ();
262275 $ collection ->setFlag ('has_stock_status_filter ' , true );
263276 $ collection ->setStore ($ storeId )
@@ -288,7 +301,7 @@ private function addItemToCart(OrderItemInterface $orderItem, Quote $cart, Produ
288301 try {
289302 $ infoBuyRequest ->setAddToCartInvalidProduct ($ this ->addToCartInvalidProduct );
290303 $ addProductResult = $ cart ->addProduct ($ product , $ infoBuyRequest );
291- } catch (\ Magento \ Framework \ Exception \ LocalizedException $ e ) {
304+ } catch (LocalizedException $ e ) {
292305 $ this ->addError ($ this ->getCartItemErrorMessage ($ orderItem , $ product , $ e ->getMessage ()));
293306 } catch (\Throwable $ e ) {
294307 $ this ->logger ->critical ($ e );
@@ -391,4 +404,15 @@ private function getCartItemErrorMessage(Item $item, Product $product, string $m
391404 ? __ ('Could not add the product with SKU "%1" to the shopping cart: %2 ' , $ sku , $ message )
392405 : __ ('Could not add the product with SKU "%1" to the shopping cart ' , $ sku ));
393406 }
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+ }
394418}
0 commit comments