Skip to content

Commit 490236c

Browse files
committed
ACP2E-368: Issue when adding multiple items in addProductsToCart graphQL mutation
1 parent c27eb17 commit 490236c

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

app/code/Magento/Quote/Model/Cart/AddProductsToCart.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ public function execute(string $maskedCartId, array $cartItems): AddProductsToCa
104104

105105
foreach ($cartItems as $cartItemPosition => $cartItem) {
106106
$this->addItemToCart($cart, $cartItem, $cartItemPosition);
107+
// reset cart items and addresses to clean cache
108+
$cart->setTotalsCollectedFlag(false);
109+
$cart->getItemsCollection()->clear();
110+
$cart->getAddressesCollection()->clear();
111+
foreach ($cart->getAddressesCollection() as $item) {
112+
$item->setQuote($cart);
113+
}
107114
}
108115

109116
if ($cart->getData('has_error')) {
@@ -118,8 +125,6 @@ public function execute(string $maskedCartId, array $cartItems): AddProductsToCa
118125
if (count($this->errors) !== 0) {
119126
/* Revert changes introduced by add to cart processes in case of an error */
120127
$cart->getItemsCollection()->clear();
121-
} else {
122-
$this->cartRepository->save($cart);
123128
}
124129

125130
return $this->prepareErrorOutput($cart);
@@ -155,6 +160,7 @@ private function addItemToCart(CartInterface $cart, Data\CartItem $cartItem, int
155160

156161
try {
157162
$result = $cart->addProduct($product, $this->requestBuilder->build($cartItem));
163+
$this->cartRepository->save($cart);
158164
} catch (\Throwable $e) {
159165
$this->addError(
160166
__($e->getMessage())->render(),

0 commit comments

Comments
 (0)