Skip to content

Commit 3b324c9

Browse files
committed
ACP2E-926: Cart is getting cleared while creating new order from admin
1 parent 2125a21 commit 3b324c9

File tree

4 files changed

+150
-8
lines changed

4 files changed

+150
-8
lines changed

app/code/Magento/Sales/Block/Adminhtml/Order/Create/Sidebar/Cart.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ public function getItemCollection()
5959
$collection = $this->getData('item_collection');
6060
if ($collection === null) {
6161
$collection = $this->getCreateOrderModel()->getCustomerCart()->getAllVisibleItems();
62+
$transferredItems = $this->getCreateOrderModel()->getSession()->getTransferredItems() ?? [];
63+
$transferredItems = $transferredItems[$this->getDataId()] ?? [];
64+
if (!empty($transferredItems)) {
65+
foreach ($collection as $key => $item) {
66+
if (in_array($item->getId(), $transferredItems)) {
67+
unset($collection[$key]);
68+
}
69+
}
70+
}
71+
6272
$this->setData('item_collection', $collection);
6373
}
6474
return $collection;

app/code/Magento/Sales/Block/Adminhtml/Order/Create/Sidebar/Wishlist.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ public function getItemCollection()
5555
$collection = $this->getCreateOrderModel()->getCustomerWishlist(true);
5656
if ($collection) {
5757
$collection = $collection->getItemCollection()->load();
58+
$transferredItems = $this->getCreateOrderModel()->getSession()->getTransferredItems() ?? [];
59+
$transferredItems = $transferredItems[$this->getDataId()] ?? [];
60+
if (!empty($transferredItems)) {
61+
foreach ($collection as $key => $item) {
62+
if (in_array($item->getId(), $transferredItems)) {
63+
$collection->removeItemByKey($key);
64+
}
65+
}
66+
}
5867
}
5968
$this->setData('item_collection', $collection);
6069
}

app/code/Magento/Sales/Model/AdminOrder/Create.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,9 @@ public function applySidebarData($data)
968968
$item = $this->getCustomerCart()->getItemById($itemId);
969969
if ($item) {
970970
$this->moveQuoteItem($item, 'order', $qty);
971-
$this->removeItem($itemId, 'cart');
971+
$transferredItems = $this->_session->getTransferredItems() ?? [];
972+
$transferredItems['cart'][] = $itemId;
973+
$this->_session->setTransferredItems($transferredItems) ;
972974
}
973975
}
974976
}
@@ -982,7 +984,9 @@ public function applySidebarData($data)
982984
);
983985
if ($item->getId()) {
984986
$this->addProduct($item->getProduct(), $item->getBuyRequest()->toArray());
985-
$this->removeItem($itemId, 'wishlist');
987+
$transferredItems = $this->_session->getTransferredItems() ?? [];
988+
$transferredItems['wishlist'][] = $itemId;
989+
$this->_session->setTransferredItems($transferredItems) ;
986990
}
987991
}
988992
}
@@ -1175,6 +1179,7 @@ public function updateQuoteItems($items)
11751179
* @throws \Magento\Framework\Exception\LocalizedException
11761180
*
11771181
* @deprecated 101.0.0
1182+
* @see void
11781183
*/
11791184
protected function _parseOptions(\Magento\Quote\Model\Quote\Item $item, $additionalOptions)
11801185
{
@@ -1245,6 +1250,7 @@ protected function _parseOptions(\Magento\Quote\Model\Quote\Item $item, $additio
12451250
* @return $this
12461251
*
12471252
* @deprecated 101.0.0
1253+
* @see void
12481254
*/
12491255
protected function _assignOptionsToItem(\Magento\Quote\Model\Quote\Item $item, $options)
12501256
{
@@ -2014,6 +2020,19 @@ public function createOrder()
20142020

20152021
$this->_eventManager->dispatch('checkout_submit_all_after', ['order' => $order, 'quote' => $quote]);
20162022

2023+
try {
2024+
if (is_array($this->getSession()->getTransferredItems())) {
2025+
foreach ($this->getSession()->getTransferredItems() as $from => $itemIds) {
2026+
foreach ($itemIds as $itemId) {
2027+
$this->removeItem($itemId, $from);
2028+
}
2029+
}
2030+
$this->recollectCart();
2031+
}
2032+
} catch (\Throwable $exception) {
2033+
$this->_logger->error($exception);
2034+
}
2035+
20172036
return $order;
20182037
}
20192038

dev/tests/integration/testsuite/Magento/Sales/Controller/Adminhtml/Order/Create/LoadBlockTest.php

Lines changed: 110 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
use Magento\Backend\Model\Session\Quote;
1111
use Magento\Framework\App\Request\Http;
1212
use Magento\Framework\Exception\NoSuchEntityException;
13+
use Magento\Framework\Message\MessageInterface;
1314
use Magento\Framework\View\LayoutInterface;
1415
use Magento\Quote\Api\CartRepositoryInterface;
1516
use Magento\Quote\Api\Data\CartInterface;
1617
use Magento\Store\Model\StoreManagerInterface;
18+
use Magento\TestFramework\Helper\Bootstrap;
1719
use Magento\TestFramework\Quote\Model\GetQuoteByReservedOrderId;
1820
use Magento\TestFramework\TestCase\AbstractBackendController;
1921
use Magento\Wishlist\Model\Wishlist;
@@ -25,6 +27,7 @@
2527
*
2628
* @magentoAppArea adminhtml
2729
* @magentoDbIsolation enabled
30+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2831
*/
2932
class LoadBlockTest extends AbstractBackendController
3033
{
@@ -95,22 +98,31 @@ public function testAddProductToOrderFromShoppingCart(bool $asJson, bool $asJsVa
9598
'json' => $asJson,
9699
'as_js_varname' => $asJsVarname,
97100
]);
101+
$itemId = $oldQuote->getItemsCollection()->getFirstItem()->getId();
98102
$post = $this->hydratePost([
99103
'sidebar' => [
100104
'add_cart_item' => [
101-
$oldQuote->getItemsCollection()->getFirstItem()->getId() => 1,
105+
$itemId => 1,
102106
],
103107
],
104108
]);
105109

106110
$this->dispatchWitParams($params, $post);
107111

108112
$this->checkHandles(explode(',', $params['block']), $asJson);
109-
$this->checkQuotes($oldQuote, 'simple2');
110113

114+
$newQuote = $this->session->getQuote();
115+
$newQuoteItemsCollection = $newQuote->getItemsCollection(false);
116+
$this->assertNotNull($newQuoteItemsCollection->getItemByColumnValue('sku', 'simple2'));
111117
if ($asJsVarname) {
112118
$this->assertRedirect($this->stringContains('sales/order_create/showUpdateResult'));
119+
$body = (string) $this->_objectManager->get(\Magento\Backend\Model\Session::class)->getUpdateResult();
120+
} elseif ($asJson) {
121+
$body = json_decode($this->getResponse()->getBody(), true, 512, JSON_THROW_ON_ERROR)['sidebar'];
122+
} else {
123+
$body = $this->getResponse()->getBody();
113124
}
125+
$this->assertStringNotContainsString("sidebar[add_cart_item][$itemId]", $body);
114126
}
115127

116128
/**
@@ -207,26 +219,31 @@ public function testMoveFromOrderToShoppingCart(): void
207219
*
208220
* @return void
209221
* @magentoDataFixture Magento/Wishlist/_files/wishlist_with_simple_product.php
222+
* @magentoDbIsolation disabled
210223
*/
211224
public function testAddProductToOrderFromWishList(): void
212225
{
213226
/** @var Wishlist $wishlist */
214227
$wishlist = $this->_objectManager->create(Wishlist::class);
215228
$wishlistItems = $wishlist->loadByCustomerId(1)->getItemCollection();
216229
$this->assertCount(1, $wishlistItems);
230+
$itemId = $wishlistItems->getFirstItem()->getId();
217231

218232
$post = $this->hydratePost([
219233
'sidebar' => [
220234
'add_wishlist_item' => [
221-
$wishlistItems->getFirstItem()->getId() => 1,
235+
$itemId => 1,
222236
],
223237
],
224238
]);
225-
$params = $this->hydrateParams();
239+
$params = $this->hydrateParams([
240+
'json' => false,
241+
'as_js_varname' => false,
242+
]);
226243
$this->dispatchWitParams($params, $post);
227244

228-
$wishlistItems->clear()->load();
229-
$this->assertEmpty($wishlistItems);
245+
$body = $this->getResponse()->getBody();
246+
$this->assertStringNotContainsString("sidebar[add_wishlist_item][$itemId]", $body);
230247
$quoteItems = $this->session->getQuote()->getItemsCollection();
231248
$this->assertCount(1, $quoteItems);
232249
}
@@ -283,6 +300,93 @@ public function testSetSpecificStoreIdIntoCurrentStore()
283300
$this->assertEquals('fixture_second_store', $this->storeManager->getStore()->getCode());
284301
}
285302

303+
/**
304+
* @magentoDataFixture Magento/Checkout/_files/quote_with_address.php
305+
*/
306+
public function testThatItemsTransferredFromShoppingCartAreDeletedAfterOrderIsCreated(): void
307+
{
308+
$oldQuote = $this->getQuoteByReservedOrderId->execute('test_order_1');
309+
$this->assertNotEmpty($oldQuote->getItemsCollection(false)->getItems());
310+
$itemId = $oldQuote->getItemsCollection()->getFirstItem()->getId();
311+
$params = $this->hydrateParams();
312+
$post = $this->hydratePost([
313+
'sidebar' => [
314+
'add_cart_item' => [
315+
$itemId => 1,
316+
],
317+
],
318+
]);
319+
320+
$this->dispatchWitParams($params, $post);
321+
$this->assertNotEmpty($oldQuote->getItemsCollection(false)->getItems());
322+
$this->placeOrder();
323+
$this->assertEmpty($oldQuote->getItemsCollection(false)->getItems());
324+
}
325+
326+
/**
327+
* @magentoDataFixture Magento/Wishlist/_files/wishlist_with_simple_product.php
328+
* @magentoDataFixture Magento/Customer/_files/customer_address.php
329+
*/
330+
public function testThatItemsTransferredFromWishlistAreDeletedAfterOrderIsCreated(): void
331+
{
332+
/** @var Wishlist $wishlist */
333+
$wishlist = $this->_objectManager->create(Wishlist::class);
334+
$wishlistItems = $wishlist->loadByCustomerId(1)->getItemCollection();
335+
$this->assertCount(1, $wishlistItems);
336+
$itemId = $wishlistItems->getFirstItem()->getId();
337+
338+
$post = $this->hydratePost([
339+
'sidebar' => [
340+
'add_wishlist_item' => [
341+
$itemId => 1,
342+
],
343+
],
344+
]);
345+
$params = $this->hydrateParams();
346+
$this->dispatchWitParams($params, $post);
347+
$wishlist = $this->_objectManager->create(Wishlist::class);
348+
$wishlistItems = $wishlist->loadByCustomerId(1)->getItemCollection();
349+
$this->assertCount(1, $wishlistItems);
350+
$this->placeOrder();
351+
$wishlist = $this->_objectManager->create(Wishlist::class);
352+
$wishlistItems = $wishlist->loadByCustomerId(1)->getItemCollection();
353+
$this->assertCount(0, $wishlistItems);
354+
}
355+
356+
/**
357+
* @return void
358+
* @throws \Magento\Framework\Exception\LocalizedException
359+
*/
360+
private function placeOrder(): void
361+
{
362+
$this->_request = null;
363+
$this->_response = null;
364+
Bootstrap::getInstance()->getBootstrap()->getApplication()->reinitialize();
365+
Bootstrap::getInstance()->loadArea('adminhtml');
366+
$this->_objectManager = Bootstrap::getObjectManager();
367+
$this->getRequest()
368+
->setMethod(\Magento\Framework\App\Request\Http::METHOD_POST)
369+
->setPostValue([
370+
'order' => [
371+
'account' => [
372+
'email' => 'john.doe001@test.com',
373+
],
374+
'shipping_method' => 'flatrate_flatrate',
375+
'payment_method' => 'checkmo',
376+
],
377+
'collect_shipping_rates' => true
378+
]);
379+
$this->dispatch('backend/sales/order_create/save');
380+
$this->assertSessionMessages(
381+
$this->isEmpty(),
382+
MessageInterface::TYPE_ERROR
383+
);
384+
$this->assertSessionMessages(
385+
$this->equalTo([(string)__('You created the order.')]),
386+
MessageInterface::TYPE_SUCCESS
387+
);
388+
}
389+
286390
/**
287391
* Check customer quotes
288392
*

0 commit comments

Comments
 (0)