@@ -36,6 +36,7 @@ class AddProductsToCart
36
36
* List of error messages and codes.
37
37
*/
38
38
private const MESSAGE_CODES = [
39
+ 'Could not find a product with SKU ' => self ::ERROR_PRODUCT_NOT_FOUND ,
39
40
'The required options you selected are not available ' => self ::ERROR_NOT_SALABLE ,
40
41
'Product that you are trying to add is not available ' => self ::ERROR_NOT_SALABLE ,
41
42
'This product is out of stock ' => self ::ERROR_NOT_SALABLE ,
@@ -128,13 +129,18 @@ private function addItemToCart(CartInterface $cart, Data\CartItem $cartItem, int
128
129
{
129
130
$ sku = $ cartItem ->getSku ();
130
131
132
+ if ($ cartItem ->getQuantity () <= 0 ) {
133
+ $ this ->addError (__ ('The product quantity should be greater than 0 ' )->render ());
134
+
135
+ return ;
136
+ }
137
+
131
138
try {
132
139
$ product = $ this ->productRepository ->get ($ sku , false , null , true );
133
140
} catch (NoSuchEntityException $ e ) {
134
141
$ this ->addError (
135
142
__ ('Could not find a product with SKU "%sku" ' , ['sku ' => $ sku ])->render (),
136
- $ cartItemPosition ,
137
- self ::ERROR_PRODUCT_NOT_FOUND
143
+ $ cartItemPosition
138
144
);
139
145
140
146
return ;
@@ -144,10 +150,7 @@ private function addItemToCart(CartInterface $cart, Data\CartItem $cartItem, int
144
150
$ result = $ cart ->addProduct ($ product , $ this ->requestBuilder ->build ($ cartItem ));
145
151
} catch (\Throwable $ e ) {
146
152
$ 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 (),
151
154
$ cartItemPosition
152
155
);
153
156
return ;
@@ -166,14 +169,13 @@ private function addItemToCart(CartInterface $cart, Data\CartItem $cartItem, int
166
169
*
167
170
* @param string $message
168
171
* @param int $cartItemPosition
169
- * @param string|null $code
170
172
* @return void
171
173
*/
172
- private function addError (string $ message , int $ cartItemPosition = 0 , string $ code = '' ): void
174
+ private function addError (string $ message , int $ cartItemPosition = 0 ): void
173
175
{
174
176
$ this ->errors [] = new Data \Error (
175
177
$ message ,
176
- $ code ?? $ this ->getErrorCode ($ message ),
178
+ $ this ->getErrorCode ($ message ),
177
179
$ cartItemPosition
178
180
);
179
181
}
0 commit comments