Skip to content

Commit f5f6b59

Browse files
committed
Fixed error handling issues
1 parent 040663f commit f5f6b59

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class AddProductsToCart
3636
* List of error messages and codes.
3737
*/
3838
private const MESSAGE_CODES = [
39+
'Could not find a product with SKU' => self::ERROR_PRODUCT_NOT_FOUND,
3940
'The required options you selected are not available' => self::ERROR_NOT_SALABLE,
4041
'Product that you are trying to add is not available' => self::ERROR_NOT_SALABLE,
4142
'This product is out of stock' => self::ERROR_NOT_SALABLE,
@@ -128,13 +129,18 @@ private function addItemToCart(CartInterface $cart, Data\CartItem $cartItem, int
128129
{
129130
$sku = $cartItem->getSku();
130131

132+
if ($cartItem->getQuantity() <= 0) {
133+
$this->addError(__('The product quantity should be greater than 0')->render());
134+
135+
return;
136+
}
137+
131138
try {
132139
$product = $this->productRepository->get($sku, false, null, true);
133140
} catch (NoSuchEntityException $e) {
134141
$this->addError(
135142
__('Could not find a product with SKU "%sku"', ['sku' => $sku])->render(),
136-
$cartItemPosition,
137-
self::ERROR_PRODUCT_NOT_FOUND
143+
$cartItemPosition
138144
);
139145

140146
return;
@@ -144,10 +150,7 @@ private function addItemToCart(CartInterface $cart, Data\CartItem $cartItem, int
144150
$result = $cart->addProduct($product, $this->requestBuilder->build($cartItem));
145151
} catch (\Throwable $e) {
146152
$this->addError(
147-
__(
148-
'Could not add the product with SKU %sku to the shopping cart: %message',
149-
['sku' => $sku, 'message' => $e->getMessage()]
150-
)->render(),
153+
__($e->getMessage())->render(),
151154
$cartItemPosition
152155
);
153156
return;
@@ -166,14 +169,13 @@ private function addItemToCart(CartInterface $cart, Data\CartItem $cartItem, int
166169
*
167170
* @param string $message
168171
* @param int $cartItemPosition
169-
* @param string|null $code
170172
* @return void
171173
*/
172-
private function addError(string $message, int $cartItemPosition = 0, string $code = ''): void
174+
private function addError(string $message, int $cartItemPosition = 0): void
173175
{
174176
$this->errors[] = new Data\Error(
175177
$message,
176-
$code ?? $this->getErrorCode($message),
178+
$this->getErrorCode($message),
177179
$cartItemPosition
178180
);
179181
}

app/code/Magento/QuoteGraphQl/Model/Resolver/AddProductsToCart.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ function (Error $error) {
8383
return [
8484
'code' => $error->getCode(),
8585
'message' => $error->getMessage(),
86-
'path' => $error->getCartItemPosition()
86+
'path' => [$error->getCartItemPosition()]
8787
];
8888
},
8989
$addProductsToCartOutput->getErrors()

0 commit comments

Comments
 (0)