Skip to content

Commit 10ffde7

Browse files
committed
ACP2E-368: Issue when adding multiple items in addProductsToCart graphQL mutation
1 parent 26ba03d commit 10ffde7

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,16 @@ public function execute(string $maskedCartId, array $cartItems): AddProductsToCa
106106
}
107107

108108
$failedCartItems = $this->addItemsToCart($cart, $cartItems);
109-
$saveCart = !empty($cartItems) && empty($failedCartItems);
109+
$saveCart = empty($failedCartItems);
110110
if (!empty($failedCartItems)) {
111-
/* Revert changes introduced by add to cart processes in case of an error */
112-
$cart->getItemsCollection()->clear();
113-
$newFailedCartItems = $this->addItemsToCart($cart, array_diff_key($cartItems, $failedCartItems));
114-
$failedCartItems += $newFailedCartItems;
115-
$saveCart = empty($newFailedCartItems);
111+
/* Check if some cart items were successfully added to the cart */
112+
if (count($failedCartItems) < count($cartItems)) {
113+
/* Revert changes introduced by add to cart processes in case of an error */
114+
$cart->getItemsCollection()->clear();
115+
$newFailedCartItems = $this->addItemsToCart($cart, array_diff_key($cartItems, $failedCartItems));
116+
$failedCartItems += $newFailedCartItems;
117+
$saveCart = empty($newFailedCartItems);
118+
}
116119
foreach (array_keys($cartItems) as $cartItemPosition) {
117120
if (isset($failedCartItems[$cartItemPosition])) {
118121
array_push($allErrors, ...$failedCartItems[$cartItemPosition]);
@@ -169,7 +172,10 @@ private function addItemToCart(Quote $cart, Data\CartItem $cartItem, int $cartIt
169172
$product = null;
170173

171174
if ($cartItem->getQuantity() <= 0) {
172-
$errors[] = $this->createError(__('The product quantity should be greater than 0')->render());
175+
$errors[] = $this->createError(
176+
__('The product quantity should be greater than 0')->render(),
177+
$cartItemPosition
178+
);
173179
} else {
174180
try {
175181
$product = $this->productRepository->get($sku, false, null, true);
@@ -202,7 +208,7 @@ private function addItemToCart(Quote $cart, Data\CartItem $cartItem, int $cartIt
202208
}
203209

204210
/**
205-
* Add order line item error
211+
* Returns an error object
206212
*
207213
* @param string $message
208214
* @param int $cartItemPosition

0 commit comments

Comments
 (0)