Skip to content

Commit 4c0454b

Browse files
committed
Merge branch 'ACP2E-3004' of https://github.com/adobe-commerce-tier-4/magento2ce into PR-VK-2024-06-10-CE
2 parents 3a7c4d1 + b213b6e commit 4c0454b

File tree

2 files changed

+418
-13
lines changed

2 files changed

+418
-13
lines changed

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

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88

99
use Magento\Catalog\Api\Data\ProductInterface;
1010
use Magento\Catalog\Model\Product;
11-
use Magento\Catalog\Model\ResourceModel\Product\Collection;
1211
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
1312
use Magento\Framework\DataObject;
13+
use Magento\Framework\Exception\AlreadyExistsException;
1414
use Magento\Framework\Exception\InputException;
15+
use Magento\Framework\Exception\LocalizedException;
1516
use Magento\Framework\Exception\NoSuchEntityException;
1617
use Magento\Quote\Api\CartRepositoryInterface;
1718
use Magento\Quote\Api\Data\CartInterface;
@@ -24,12 +25,16 @@
2425
use Magento\Sales\Model\OrderFactory;
2526
use Magento\Framework\App\ObjectManager;
2627
use Magento\Store\Model\StoreManagerInterface;
28+
use Magento\Framework\Exception\CouldNotSaveException;
2729
use Magento\Sales\Model\ResourceModel\Order\Item\Collection as ItemCollection;
30+
use Magento\Customer\Model\Session as CustomerSession;
2831
use Psr\Log\LoggerInterface;
2932

3033
/**
3134
* Allows customer quickly to reorder previously added products and put them to the Cart
35+
*
3236
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
37+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
3338
*/
3439
class Reorder
3540
{
@@ -116,6 +121,11 @@ class Reorder
116121
*/
117122
private bool $addToCartInvalidProduct;
118123

124+
/**
125+
* @var CustomerSession
126+
*/
127+
private $customerSession;
128+
119129
/**
120130
* @param OrderFactory $orderFactory
121131
* @param CustomerCartResolver $customerCartProvider
@@ -127,7 +137,7 @@ class Reorder
127137
* @param OrderInfoBuyRequestGetter $orderInfoBuyRequestGetter
128138
* @param StoreManagerInterface|null $storeManager
129139
* @param bool $addToCartInvalidProduct
130-
*
140+
* @param CustomerSession|null $customerSession
131141
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
132142
*/
133143
public function __construct(
@@ -140,7 +150,8 @@ public function __construct(
140150
ProductCollectionFactory $productCollectionFactory,
141151
OrderInfoBuyRequestGetter $orderInfoBuyRequestGetter,
142152
?StoreManagerInterface $storeManager = null,
143-
bool $addToCartInvalidProduct = false
153+
bool $addToCartInvalidProduct = false,
154+
?CustomerSession $customerSession = null
144155
) {
145156
$this->orderFactory = $orderFactory;
146157
$this->cartRepository = $cartRepository;
@@ -153,6 +164,8 @@ public function __construct(
153164
$this->storeManager = $storeManager
154165
?: ObjectManager::getInstance()->get(StoreManagerInterface::class);
155166
$this->addToCartInvalidProduct = $addToCartInvalidProduct;
167+
$this->customerSession = $customerSession
168+
?: ObjectManager::getInstance()->get(CustomerSession::class);
156169
}
157170

158171
/**
@@ -163,7 +176,9 @@ public function __construct(
163176
* @return Data\ReorderOutput
164177
* @throws InputException Order is not found
165178
* @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
167182
*/
168183
public function execute(string $orderNumber, string $storeId): Data\ReorderOutput
169184
{
@@ -174,10 +189,10 @@ public function execute(string $orderNumber, string $storeId): Data\ReorderOutpu
174189
__('Cannot find order number "%1" in store "%2"', $orderNumber, $storeId)
175190
);
176191
}
177-
$customerId = (int)$order->getCustomerId();
192+
$customerId = (int) $order->getCustomerId();
178193
$this->errors = [];
179194

180-
$cart = $customerId === 0
195+
$cart = $this->isCustomerReorderAsGuest($customerId)
181196
? $this->guestCartResolver->resolve()
182197
: $this->customerCartProvider->resolve($customerId);
183198
if (!$this->reorderHelper->isAllowed($order->getStore())) {
@@ -190,7 +205,7 @@ public function execute(string $orderNumber, string $storeId): Data\ReorderOutpu
190205

191206
try {
192207
$this->cartRepository->save($cart);
193-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
208+
} catch (LocalizedException $e) {
194209
// handle exception from \Magento\Quote\Model\QuoteRepository\SaveHandler::save
195210
$this->addError($e->getMessage());
196211
}
@@ -207,14 +222,15 @@ public function execute(string $orderNumber, string $storeId): Data\ReorderOutpu
207222
* @param ItemCollection $orderItems
208223
* @param string $storeId
209224
* @return void
225+
* @throws LocalizedException
210226
*/
211227
private function addItemsToCart(Quote $cart, ItemCollection $orderItems, string $storeId): void
212228
{
213229
$orderItemProductIds = [];
214-
/** @var \Magento\Sales\Model\Order\Item[] $orderItemsByProductId */
230+
/** @var Item[] $orderItemsByProductId */
215231
$orderItemsByProductId = [];
216232

217-
/** @var \Magento\Sales\Model\Order\Item $item */
233+
/** @var Item $item */
218234
foreach ($orderItems as $item) {
219235
if ($item->getParentItem() === null) {
220236
$orderItemProductIds[] = $item->getProductId();
@@ -228,7 +244,7 @@ private function addItemsToCart(Quote $cart, ItemCollection $orderItems, string
228244
$productsNotFound = array_diff($orderItemProductIds, array_keys($products));
229245
if (!empty($productsNotFound)) {
230246
foreach ($productsNotFound as $productId) {
231-
/** @var \Magento\Sales\Model\Order\Item $orderItemProductNotFound */
247+
/** @var Item $orderItemProductNotFound */
232248
$this->addError(
233249
(string)__('Could not find a product with ID "%1"', $productId),
234250
self::ERROR_PRODUCT_NOT_FOUND
@@ -253,11 +269,10 @@ private function addItemsToCart(Quote $cart, ItemCollection $orderItems, string
253269
* @param string $storeId
254270
* @param int[] $orderItemProductIds
255271
* @return Product[]
256-
* @throws \Magento\Framework\Exception\LocalizedException
272+
* @throws LocalizedException
257273
*/
258274
private function getOrderProducts(string $storeId, array $orderItemProductIds): array
259275
{
260-
/** @var Collection $collection */
261276
$collection = $this->productCollectionFactory->create();
262277
$collection->setFlag('has_stock_status_filter', true);
263278
$collection->setStore($storeId)
@@ -288,7 +303,7 @@ private function addItemToCart(OrderItemInterface $orderItem, Quote $cart, Produ
288303
try {
289304
$infoBuyRequest->setAddToCartInvalidProduct($this->addToCartInvalidProduct);
290305
$addProductResult = $cart->addProduct($product, $infoBuyRequest);
291-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
306+
} catch (LocalizedException $e) {
292307
$this->addError($this->getCartItemErrorMessage($orderItem, $product, $e->getMessage()));
293308
} catch (\Throwable $e) {
294309
$this->logger->critical($e);
@@ -391,4 +406,15 @@ private function getCartItemErrorMessage(Item $item, Product $product, string $m
391406
? __('Could not add the product with SKU "%1" to the shopping cart: %2', $sku, $message)
392407
: __('Could not add the product with SKU "%1" to the shopping cart', $sku));
393408
}
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+
}
394420
}

0 commit comments

Comments
 (0)