Skip to content

Commit 1d306a7

Browse files
committed
ACP2E-2518: Reorder adds non assigned product to cart
- updated changes
1 parent 03621bb commit 1d306a7

File tree

2 files changed

+136
-1
lines changed

2 files changed

+136
-1
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use Magento\Sales\Helper\Reorder as ReorderHelper;
2323
use Magento\Sales\Model\Order\Item;
2424
use Magento\Sales\Model\OrderFactory;
25+
use Magento\Framework\App\ObjectManager;
26+
use Magento\Store\Model\StoreManagerInterface;
2527
use Magento\Sales\Model\ResourceModel\Order\Item\Collection as ItemCollection;
2628
use Psr\Log\LoggerInterface;
2729

@@ -104,6 +106,11 @@ class Reorder
104106
*/
105107
private $orderInfoBuyRequestGetter;
106108

109+
/**
110+
* @var StoreManagerInterface
111+
*/
112+
private $storeManager;
113+
107114
/**
108115
* @param OrderFactory $orderFactory
109116
* @param CustomerCartResolver $customerCartProvider
@@ -113,6 +120,7 @@ class Reorder
113120
* @param LoggerInterface $logger
114121
* @param ProductCollectionFactory $productCollectionFactory
115122
* @param OrderInfoBuyRequestGetter $orderInfoBuyRequestGetter
123+
* @param StoreManagerInterface|null $storeManager
116124
*/
117125
public function __construct(
118126
OrderFactory $orderFactory,
@@ -122,7 +130,8 @@ public function __construct(
122130
ReorderHelper $reorderHelper,
123131
LoggerInterface $logger,
124132
ProductCollectionFactory $productCollectionFactory,
125-
OrderInfoBuyRequestGetter $orderInfoBuyRequestGetter
133+
OrderInfoBuyRequestGetter $orderInfoBuyRequestGetter,
134+
?StoreManagerInterface $storeManager = null
126135
) {
127136
$this->orderFactory = $orderFactory;
128137
$this->cartRepository = $cartRepository;
@@ -132,6 +141,8 @@ public function __construct(
132141
$this->guestCartResolver = $guestCartResolver;
133142
$this->productCollectionFactory = $productCollectionFactory;
134143
$this->orderInfoBuyRequestGetter = $orderInfoBuyRequestGetter;
144+
$this->storeManager = $storeManager
145+
?: ObjectManager::getInstance()->get(StoreManagerInterface::class);
135146
}
136147

137148
/**
@@ -164,6 +175,7 @@ public function execute(string $orderNumber, string $storeId): Data\ReorderOutpu
164175
return $this->prepareOutput($cart);
165176
}
166177

178+
$storeId = (string) $this->storeManager->getStore()->getId();
167179
$this->addItemsToCart($cart, $order->getItemsCollection(), $storeId);
168180

169181
try {
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
/************************************************************************
3+
*
4+
* ADOBE CONFIDENTIAL
5+
* ___________________
6+
*
7+
* Copyright 2023 Adobe
8+
* All Rights Reserved.
9+
*
10+
* NOTICE: All information contained herein is, and remains
11+
* the property of Adobe and its suppliers, if any. The intellectual
12+
* and technical concepts contained herein are proprietary to Adobe
13+
* and its suppliers and are protected by all applicable intellectual
14+
* property laws, including trade secret and copyright laws.
15+
* Dissemination of this information or reproduction of this material
16+
* is strictly forbidden unless prior written permission is obtained
17+
* from Adobe.
18+
* ************************************************************************
19+
*/
20+
declare(strict_types=1);
21+
22+
namespace Magento\Sales\Controller\Order;
23+
24+
use Magento\Checkout\Model\Session as CheckoutSession;
25+
use Magento\Customer\Model\Session;
26+
use Magento\Framework\Exception\LocalizedException;
27+
use Magento\Framework\Exception\NoSuchEntityException;
28+
use Magento\Quote\Api\CartRepositoryInterface;
29+
use Magento\Quote\Api\Data\CartInterface;
30+
use Magento\Sales\Api\Data\OrderInterfaceFactory;
31+
use Magento\Store\Model\StoreManagerInterface;
32+
use Magento\TestFramework\Request;
33+
use Magento\TestFramework\TestCase\AbstractController;
34+
35+
/**
36+
* Test for reorder with different store.
37+
*
38+
* @magentoAppArea frontend
39+
* @magentoDbIsolation enabled
40+
*/
41+
class ReorderWithDifferentStoreTest extends AbstractController
42+
{
43+
/** @var CheckoutSession */
44+
private $checkoutSession;
45+
46+
/** @var OrderInterfaceFactory */
47+
private $orderFactory;
48+
49+
/** @var Session */
50+
private $customerSession;
51+
52+
/** @var CartRepositoryInterface */
53+
private $quoteRepository;
54+
55+
/** @var CartInterface */
56+
private $quote;
57+
58+
/**
59+
* @var StoreManagerInterface
60+
*/
61+
private $storeManager;
62+
63+
/**
64+
* @var int
65+
*/
66+
private $currentStoreId;
67+
68+
/**
69+
* @inheritdoc
70+
*/
71+
protected function setUp(): void
72+
{
73+
parent::setUp();
74+
75+
$this->checkoutSession = $this->_objectManager->get(CheckoutSession::class);
76+
$this->orderFactory = $this->_objectManager->get(OrderInterfaceFactory::class);
77+
$this->customerSession = $this->_objectManager->get(Session::class);
78+
$this->quoteRepository = $this->_objectManager->get(CartRepositoryInterface::class);
79+
$this->storeManager = $this->_objectManager->get(StoreManagerInterface::class);
80+
$this->currentStoreId = $this->storeManager->getStore()->getId();
81+
}
82+
83+
/**
84+
* @inheritdoc
85+
*/
86+
protected function tearDown(): void
87+
{
88+
if ($this->quote instanceof CartInterface) {
89+
$this->quoteRepository->delete($this->quote);
90+
}
91+
$this->customerSession->setCustomerId(null);
92+
$this->storeManager->setCurrentStore($this->currentStoreId);
93+
parent::tearDown();
94+
}
95+
96+
/**
97+
* Test when reorder from different store and global customer account
98+
*
99+
* @magentoConfßigFixture web/url/use_store 1
100+
* @magentoConfigFixture customer/account_share/scope 0
101+
* @magentoDataFixture Magento/Sales/_files/customer_order_with_simple_product.php
102+
* @magentoDataFixture Magento/Store/_files/second_store.php
103+
*
104+
* @return void
105+
* @throws LocalizedException
106+
* @throws NoSuchEntityException
107+
*/
108+
public function testReorderWithDifferentStoreAndGlobalCustomerAccount(): void
109+
{
110+
$this->checkoutSession->resetCheckout();
111+
$this->storeManager->setCurrentStore('fixture_second_store');
112+
$order = $this->orderFactory->create()->loadByIncrementId('55555555');
113+
$orderNumber = (int) $order->getId();
114+
$this->customerSession->setCustomerId($order->getCustomerId());
115+
$this->getRequest()->setMethod(Request::METHOD_POST);
116+
$this->getRequest()->setParam('order_id', $orderNumber);
117+
$this->dispatch('sales/order/reorder/');
118+
$this->assertRedirect($this->stringContains('checkout/cart'));
119+
$this->quote = $this->checkoutSession->getQuote();
120+
$quoteItemsCollection = $this->quote->getAllItems();
121+
$this->assertCount(0, $quoteItemsCollection);
122+
}
123+
}

0 commit comments

Comments
 (0)