@@ -38,7 +38,7 @@ class AddProductsToCart
38
38
private const MESSAGE_CODES = [
39
39
'Could not find a product with SKU ' => self ::ERROR_PRODUCT_NOT_FOUND ,
40
40
'The required options you selected are not available ' => self ::ERROR_NOT_SALABLE ,
41
- 'Product that you are trying to add is not available ' => self ::ERROR_NOT_SALABLE ,
41
+ 'Product that you are trying to add is not available. ' => self ::ERROR_NOT_SALABLE ,
42
42
'This product is out of stock ' => self ::ERROR_NOT_SALABLE ,
43
43
'There are no source items ' => self ::ERROR_NOT_SALABLE ,
44
44
'The fewest you may purchase is ' => self ::ERROR_INSUFFICIENT_STOCK ,
@@ -102,8 +102,8 @@ public function execute(string $maskedCartId, array $cartItems): AddProductsToCa
102
102
$ cartId = $ this ->maskedQuoteIdToQuoteId ->execute ($ maskedCartId );
103
103
$ cart = $ this ->cartRepository ->get ($ cartId );
104
104
105
- foreach ($ cartItems as $ n => $ cartItem ) {
106
- $ this ->addItemToCart ($ cart , $ cartItem , $ n );
105
+ foreach ($ cartItems as $ cartItemPosition => $ cartItem ) {
106
+ $ this ->addItemToCart ($ cart , $ cartItem , $ cartItemPosition );
107
107
}
108
108
if ($ cart ->getData ('has_error ' )) {
109
109
$ errors = $ cart ->getErrors ();
@@ -190,21 +190,14 @@ private function addError(string $message, int $cartItemPosition = 0): void
190
190
*/
191
191
private function getErrorCode (string $ message ): string
192
192
{
193
- $ code = self ::ERROR_UNDEFINED ;
194
-
195
- $ matchedCodes = array_filter (
196
- self ::MESSAGE_CODES ,
197
- function ($ key ) use ($ message ) {
198
- return false !== strpos ($ message , $ key );
199
- },
200
- ARRAY_FILTER_USE_KEY
201
- );
202
-
203
- if (!empty ($ matchedCodes )) {
204
- $ code = current ($ matchedCodes );
193
+ foreach (self ::MESSAGE_CODES as $ codeMessage => $ code ) {
194
+ if (false !== stripos ($ codeMessage , $ message )) {
195
+ return $ code ;
196
+ }
205
197
}
206
198
207
- return $ code ;
199
+ /* If no code was matched, return the default one */
200
+ return self ::ERROR_UNDEFINED ;
208
201
}
209
202
210
203
/**
0 commit comments