@@ -106,13 +106,16 @@ public function execute(string $maskedCartId, array $cartItems): AddProductsToCa
106
106
}
107
107
108
108
$ failedCartItems = $ this ->addItemsToCart ($ cart , $ cartItems );
109
- $ saveCart = ! empty ( $ cartItems ) && empty ($ failedCartItems );
109
+ $ saveCart = empty ($ failedCartItems );
110
110
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
+ }
116
119
foreach (array_keys ($ cartItems ) as $ cartItemPosition ) {
117
120
if (isset ($ failedCartItems [$ cartItemPosition ])) {
118
121
array_push ($ allErrors , ...$ failedCartItems [$ cartItemPosition ]);
@@ -169,7 +172,10 @@ private function addItemToCart(Quote $cart, Data\CartItem $cartItem, int $cartIt
169
172
$ product = null ;
170
173
171
174
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
+ );
173
179
} else {
174
180
try {
175
181
$ product = $ this ->productRepository ->get ($ sku , false , null , true );
@@ -202,7 +208,7 @@ private function addItemToCart(Quote $cart, Data\CartItem $cartItem, int $cartIt
202
208
}
203
209
204
210
/**
205
- * Add order line item error
211
+ * Returns an error object
206
212
*
207
213
* @param string $message
208
214
* @param int $cartItemPosition
0 commit comments