Skip to content

Commit 92bcdf1

Browse files
authored
LYNX-547: Insufficient and unavailable quantity notifications
1 parent a782aba commit 92bcdf1

File tree

12 files changed

+42
-29
lines changed

12 files changed

+42
-29
lines changed

app/code/Magento/CatalogInventory/Model/Config/Source/NotAvailableMessage.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ public function toOptionArray(): array
3030
$options = [];
3131
$options[] = [
3232
'value' => self::VALUE_ONLY_X_OF_Y,
33-
'label' => __('Only X available for sale. Please adjust the quantity to continue'),
33+
'label' => __('Only X of Y available'),
3434
];
3535
$options[] = [
3636
'value' => self::VALUE_NOT_ENOUGH_ITEMS,
37-
'label' => __('Not enough items for sale. Please adjust the quantity to continue'),
37+
'label' => __('Not enough items for sale'),
3838
];
3939
return $options;
4040
}
@@ -47,8 +47,8 @@ public function toOptionArray(): array
4747
public function toArray(): array
4848
{
4949
return [
50-
self::VALUE_ONLY_X_OF_Y => __('Only X available for sale. Please adjust the quantity to continue'),
51-
self::VALUE_NOT_ENOUGH_ITEMS => __('Not enough items for sale. Please adjust the quantity to continue')
50+
self::VALUE_ONLY_X_OF_Y => __('Only X of Y available'),
51+
self::VALUE_NOT_ENOUGH_ITEMS => __('Not enough items for sale')
5252
];
5353
}
5454
}

app/code/Magento/CatalogInventory/Model/StockStateProvider.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,15 @@ public function checkQuoteItemQty(StockItemInterface $stockItem, $qty, $summaryQ
167167
}
168168

169169
if (!$this->checkQty($stockItem, $summaryQty) || !$this->checkQty($stockItem, $qty)) {
170-
$message = __('The requested qty is not available');
170+
$message = __('The requested qty. is not available');
171171
if ((int) $this->scopeConfig->getValue('cataloginventory/options/not_available_message') === 1) {
172172
$itemMessage = (__(sprintf(
173-
'Only %s available for sale. Please adjust the quantity to continue',
174-
$stockItem->getQty() - $stockItem->getMinQty()
173+
'Only %s of %s available',
174+
$stockItem->getQty() - $stockItem->getMinQty(),
175+
$this->localeFormat->getNumber($qty)
175176
)));
176177
} else {
177-
$itemMessage = (__('Not enough items for sale. Please adjust the quantity to continue'));
178+
$itemMessage = (__('Not enough items for sale'));
178179
}
179180
$result->setHasError(true)
180181
->setErrorCode('qty_available')
@@ -231,7 +232,7 @@ public function checkQuoteItemQty(StockItemInterface $stockItem, $qty, $summaryQ
231232
}
232233
} elseif ($stockItem->getShowDefaultNotificationMessage()) {
233234
$result->setMessage(
234-
__('The requested qty is not available')
235+
__('The requested qty. is not available')
235236
);
236237
}
237238
}

app/code/Magento/CatalogInventory/i18n/en_US.csv

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ Stock,Stock
7373
"Done","Done"
7474
"The requested qty exceeds the maximum qty allowed in shopping cart","The requested qty exceeds the maximum qty allowed in shopping cart"
7575
"You cannot use decimal quantity for this product.","You cannot use decimal quantity for this product."
76-
"Not enough items for sale. Please adjust the quantity to continue","Not enough items for sale. Please adjust the quantity to continue"
77-
"Only X available for sale. Please adjust the quantity to continue","Only X available for sale. Please adjust the quantity to continue"
78-
"Only %s available for sale. Please adjust the quantity to continue","Only %s available for sale. Please adjust the quantity to continue"
76+
"Only X of Y available","Only X of Y available"
77+
"Only %s of %s available","Only %s of %s available"
78+
"Not enough items for sale","Not enough items for sale"
79+
"The requested qty. is not available","The requested qty. is not available"

app/code/Magento/CatalogInventoryGraphQl/Model/Resolver/NotAvailableMessageResolver.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
4747
}
4848

4949
if ((int) $this->scopeConfig->getValue('cataloginventory/options/not_available_message') === 1) {
50+
$requiredItemQty = ($cartItem->getQtyToAdd() ?? $cartItem->getQty()) + ($cartItem->getPreviousQty() ?? 0);
5051
return sprintf(
51-
'Only %s available for sale. Please adjust the quantity to continue',
52-
(string) $this->productStock->getProductSaleableQty($cartItem)
52+
'Only %s of %s available',
53+
(string) $this->productStock->getProductSaleableQty($cartItem),
54+
(string) $requiredItemQty
5355
);
5456
}
5557

56-
return 'Not enough items for sale. Please adjust the quantity to continue';
58+
return 'Not enough items for sale';
5759
}
5860
}

app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ public function execute(Quote $cart, array $cartItemData): void
6464
try {
6565
$result = $cart->addProduct($product, $this->buyRequestBuilder->build($cartItemData));
6666
} catch (Exception $e) {
67+
68+
if (str_contains($e->getMessage(), 'The requested qty is not available')) {
69+
throw new GraphQlInputException(__('The requested qty. is not available'));
70+
}
71+
6772
throw new GraphQlInputException(
6873
__(
6974
'Could not add the product with SKU %sku to the shopping cart: %message',

app/code/Magento/QuoteGraphQl/Model/Cart/UpdateCartItem.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
use Magento\Quote\Model\Quote;
1717
use Magento\Quote\Model\Quote\Item;
1818

19-
/**
20-
* Update cart item
21-
*/
2219
class UpdateCartItem
2320
{
2421
/**
@@ -129,6 +126,9 @@ private function validateCartItem(Item $cartItem): void
129126
if ($cartItem->getHasError()) {
130127
$errors = [];
131128
foreach ($cartItem->getMessage(false) as $message) {
129+
if (str_contains($message, 'The requested qty is not available')) {
130+
throw new GraphQlInputException(__('The requested qty. is not available'));
131+
}
132132
$errors[] = $message;
133133
}
134134
if (!empty($errors)) {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,11 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
7979
$this->cartRepository->get((int)$cart->getId())
8080
);
8181
} catch (NoSuchEntityException | LocalizedException $e) {
82+
$message = (str_contains($e->getMessage(), 'The requested qty is not available'))
83+
? 'The requested qty. is not available'
84+
: $e->getMessage();
8285
$errors[] = [
83-
'message' => __($e->getMessage()),
86+
'message' => __($message),
8487
'code' => $this->getErrorCode($e->getMessage())
8588
];
8689
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
"""model"" value should be specified","""model"" value should be specified"
2+
"The requested qty. is not available","The requested qty. is not available"

dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogInventory/AddProductToCartTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected function setUp(): void
3737
public function testAddProductIfQuantityIsNotAvailable()
3838
{
3939
$this->expectException(\Exception::class);
40-
$this->expectExceptionMessage('The requested qty is not available');
40+
$this->expectExceptionMessage('The requested qty. is not available');
4141

4242
$sku = 'simple';
4343
$quantity = 200;

dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogInventory/UpdateCartItemsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function testUpdateCartItemSetUnavailableQuantity()
8080

8181
$responseError = $response['updateCartItems']['errors'][0];
8282
$this->assertEquals(
83-
"Could not update the product with SKU simple_product: The requested qty is not available",
83+
"The requested qty. is not available",
8484
$responseError['message']
8585
);
8686
$this->assertEquals('INSUFFICIENT_STOCK', $responseError['code']);

0 commit comments

Comments
 (0)