Skip to content

Commit 52fb7a7

Browse files
committed
Error handling performance improvements
1 parent f5f6b59 commit 52fb7a7

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

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

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class AddProductsToCart
3838
private const MESSAGE_CODES = [
3939
'Could not find a product with SKU' => self::ERROR_PRODUCT_NOT_FOUND,
4040
'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,
4242
'This product is out of stock' => self::ERROR_NOT_SALABLE,
4343
'There are no source items' => self::ERROR_NOT_SALABLE,
4444
'The fewest you may purchase is' => self::ERROR_INSUFFICIENT_STOCK,
@@ -102,8 +102,8 @@ public function execute(string $maskedCartId, array $cartItems): AddProductsToCa
102102
$cartId = $this->maskedQuoteIdToQuoteId->execute($maskedCartId);
103103
$cart = $this->cartRepository->get($cartId);
104104

105-
foreach ($cartItems as $n => $cartItem) {
106-
$this->addItemToCart($cart, $cartItem, $n);
105+
foreach ($cartItems as $cartItemPosition => $cartItem) {
106+
$this->addItemToCart($cart, $cartItem, $cartItemPosition);
107107
}
108108
if ($cart->getData('has_error')) {
109109
$errors = $cart->getErrors();
@@ -190,21 +190,14 @@ private function addError(string $message, int $cartItemPosition = 0): void
190190
*/
191191
private function getErrorCode(string $message): string
192192
{
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+
}
205197
}
206198

207-
return $code;
199+
/* If no code was matched, return the default one */
200+
return self::ERROR_UNDEFINED;
208201
}
209202

210203
/**

0 commit comments

Comments
 (0)