Skip to content

Commit 5658e50

Browse files
committed
ACP2E-42: [Magento Cloud] Null response under items list in shopping cart with out of stock product
1 parent 7900123 commit 5658e50

File tree

4 files changed

+109
-7
lines changed

4 files changed

+109
-7
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Resolver;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\Exception\RuntimeException;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Query\ResolverInterface;
14+
use Magento\Framework\GraphQl\Query\EnumLookup;
15+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
16+
use Magento\Quote\Model\Quote\Item;
17+
18+
/**
19+
* @inheritdoc
20+
*/
21+
class CartItemErrors implements ResolverInterface
22+
{
23+
/**
24+
* Error code
25+
*/
26+
private const ERROR_UNDEFINED = 0;
27+
28+
/**
29+
* @var EnumLookup
30+
*/
31+
private $enumLookup;
32+
33+
/**
34+
* @param EnumLookup $enumLookup
35+
*/
36+
public function __construct(
37+
EnumLookup $enumLookup
38+
) {
39+
$this->enumLookup = $enumLookup;
40+
}
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
46+
{
47+
if (!isset($value['model'])) {
48+
throw new LocalizedException(__('"model" value should be specified'));
49+
}
50+
/** @var Item $cartItem */
51+
$cartItem = $value['model'];
52+
53+
return $this->getItemErrors($cartItem);
54+
}
55+
56+
/**
57+
* Get error messages for cart item
58+
*
59+
* @param Item $cartItem
60+
* @return string[]|null
61+
* @throws RuntimeException
62+
*/
63+
private function getItemErrors(Item $cartItem): ?array
64+
{
65+
$hasError = (bool) $cartItem->getData('has_error');
66+
if (!$hasError) {
67+
return null;
68+
}
69+
70+
$errors = [];
71+
foreach ($cartItem->getErrorInfos() as $error) {
72+
$errorType = $error['code'] ?? self::ERROR_UNDEFINED;
73+
$message = $error['message'] ?? $cartItem->getMessage();
74+
$errorEnumCode = $this->enumLookup->getEnumValueFromField(
75+
'CartItemErrorType',
76+
(string)$errorType
77+
);
78+
$errors[] = [
79+
'code' => $errorEnumCode,
80+
'message' => $message
81+
];
82+
}
83+
84+
return $errors;
85+
}
86+
}

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,6 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
5454
$cart = $value['model'];
5555

5656
$itemsData = [];
57-
if ($cart->getData('has_error')) {
58-
$errors = $cart->getErrors();
59-
foreach ($errors as $error) {
60-
$itemsData[] = new GraphQlInputException(__($error->getText()));
61-
}
62-
}
63-
6457
$cartProductsData = $this->getCartProductsData($cart);
6558
$cartItems = $cart->getAllVisibleItems();
6659
/** @var QuoteItem $cartItem */

app/code/Magento/QuoteGraphQl/etc/graphql/di.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,15 @@
3131
<argument name="quoteTotalsCollector" xsi:type="object">Magento\Quote\Model\Quote\TotalsCollector\Proxy</argument>
3232
</arguments>
3333
</type>
34+
<type name="Magento\Framework\GraphQl\Schema\Type\Enum\DefaultDataMapper">
35+
<arguments>
36+
<argument name="map" xsi:type="array">
37+
<item name="CartItemErrorType" xsi:type="array">
38+
<item name="undefined" xsi:type="string">0</item>
39+
<item name="item_qty" xsi:type="string">1</item>
40+
<item name="item_increment" xsi:type="string">2</item>
41+
</item>
42+
</argument>
43+
</arguments>
44+
</type>
3445
</config>

app/code/Magento/QuoteGraphQl/etc/schema.graphqls

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,18 @@ interface CartItemInterface @typeResolver(class: "Magento\\QuoteGraphQl\\Model\\
339339
quantity: Float! @doc(description: "The quantity of this item in the cart.")
340340
prices: CartItemPrices @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartItemPrices") @doc(description: "Contains details about the price of the item, including taxes and discounts.")
341341
product: ProductInterface! @doc(description: "Details about an item in the cart.")
342+
errors: [CartItemError!] @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartItemErrors") @doc(description: "An array of errors encountered while loading the cart item")
343+
}
344+
345+
type CartItemError {
346+
code: CartItemErrorType! @doc(description: "An error code that describes the error encountered")
347+
message: String! @doc(description: "A localized error message")
348+
}
349+
350+
enum CartItemErrorType {
351+
UNDEFINED
352+
ITEM_QTY
353+
ITEM_INCREMENTS
342354
}
343355

344356
type Discount @doc(description:"Defines an individual discount. A discount can be applied to the cart as a whole or to an item.") {

0 commit comments

Comments
 (0)