Skip to content

Commit 9640575

Browse files
authored
ENGCOM-5772: graphQl-890: Replaced usage of the CartItemQuantity with the CartItem… #899
2 parents df5c5d9 + 48e175b commit 9640575

File tree

5 files changed

+140
-10
lines changed

5 files changed

+140
-10
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,25 @@ public function execute(QuoteAddress $address): array
6161
return $addressData;
6262
}
6363

64-
$addressItemsData = [];
6564
foreach ($address->getAllItems() as $addressItem) {
6665
if ($addressItem instanceof \Magento\Quote\Model\Quote\Item) {
6766
$itemId = $addressItem->getItemId();
6867
} else {
6968
$itemId = $addressItem->getQuoteItemId();
7069
}
71-
72-
$addressItemsData[] = [
70+
$productData = $addressItem->getProduct()->getData();
71+
$productData['model'] = $addressItem->getProduct();
72+
$addressData['cart_items'][] = [
7373
'cart_item_id' => $itemId,
7474
'quantity' => $addressItem->getQty()
7575
];
76+
$addressData['cart_items_v2'][] = [
77+
'id' => $itemId,
78+
'quantity' => $addressItem->getQty(),
79+
'product' => $productData,
80+
'model' => $addressItem,
81+
];
7682
}
77-
$addressData['cart_items'] = $addressItemsData;
78-
7983
return $addressData;
8084
}
8185
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,18 +220,19 @@ interface CartAddressInterface @typeResolver(class: "\\Magento\\QuoteGraphQl\\Mo
220220
type ShippingCartAddress implements CartAddressInterface {
221221
available_shipping_methods: [AvailableShippingMethod] @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ShippingAddress\\AvailableShippingMethods")
222222
selected_shipping_method: SelectedShippingMethod @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ShippingAddress\\SelectedShippingMethod")
223-
items_weight: Float
224-
cart_items: [CartItemQuantity]
225223
customer_notes: String
224+
items_weight: Float @deprecated(reason: "This information shoud not be exposed on frontend")
225+
cart_items: [CartItemQuantity] @deprecated(reason: "`cart_items_v2` should be used instead")
226+
cart_items_v2: [CartItemInterface]
226227
}
227228

228229
type BillingCartAddress implements CartAddressInterface {
229230
customer_notes: String @deprecated (reason: "The field is used only in shipping address")
230231
}
231232

232-
type CartItemQuantity {
233-
cart_item_id: Int!
234-
quantity: Float!
233+
type CartItemQuantity @doc(description:"Deprecated: `cart_items` field of `ShippingCartAddress` returns now `CartItemInterface` instead of `CartItemQuantity`") {
234+
cart_item_id: Int! @deprecated(reason: "`cart_items` field of `ShippingCartAddress` returns now `CartItemInterface` instead of `CartItemQuantity`")
235+
quantity: Float! @deprecated(reason: "`cart_items` field of `ShippingCartAddress` returns now `CartItemInterface` instead of `CartItemQuantity`")
235236
}
236237

237238
type CartAddressRegion {

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/AddSimpleProductToCartTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,36 @@ public function testAddSimpleProductToCart()
5050
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
5151

5252
self::assertArrayHasKey('cart', $response['addSimpleProductsToCart']);
53+
self::assertArrayHasKey('shipping_addresses', $response['addSimpleProductsToCart']['cart']);
54+
self::assertEmpty($response['addSimpleProductsToCart']['cart']['shipping_addresses']);
5355
self::assertEquals($quantity, $response['addSimpleProductsToCart']['cart']['items'][0]['quantity']);
5456
self::assertEquals($sku, $response['addSimpleProductsToCart']['cart']['items'][0]['product']['sku']);
57+
self::assertArrayHasKey('prices', $response['addSimpleProductsToCart']['cart']['items'][0]);
58+
59+
self::assertArrayHasKey('price', $response['addSimpleProductsToCart']['cart']['items'][0]['prices']);
60+
$price = $response['addSimpleProductsToCart']['cart']['items'][0]['prices']['price'];
61+
self::assertArrayHasKey('value', $price);
62+
self::assertEquals(10, $price['value']);
63+
self::assertArrayHasKey('currency', $price);
64+
self::assertEquals('USD', $price['currency']);
65+
66+
self::assertArrayHasKey('row_total', $response['addSimpleProductsToCart']['cart']['items'][0]['prices']);
67+
$rowTotal = $response['addSimpleProductsToCart']['cart']['items'][0]['prices']['row_total'];
68+
self::assertArrayHasKey('value', $rowTotal);
69+
self::assertEquals(20, $rowTotal['value']);
70+
self::assertArrayHasKey('currency', $rowTotal);
71+
self::assertEquals('USD', $rowTotal['currency']);
72+
73+
self::assertArrayHasKey(
74+
'row_total_including_tax',
75+
$response['addSimpleProductsToCart']['cart']['items'][0]['prices']
76+
);
77+
$rowTotalIncludingTax =
78+
$response['addSimpleProductsToCart']['cart']['items'][0]['prices']['row_total_including_tax'];
79+
self::assertArrayHasKey('value', $rowTotalIncludingTax);
80+
self::assertEquals(20, $rowTotalIncludingTax['value']);
81+
self::assertArrayHasKey('currency', $rowTotalIncludingTax);
82+
self::assertEquals('USD', $rowTotalIncludingTax['currency']);
5583
}
5684

5785
/**
@@ -262,6 +290,34 @@ private function getQuery(string $maskedQuoteId, string $sku, float $quantity):
262290
product {
263291
sku
264292
}
293+
prices {
294+
price {
295+
value
296+
currency
297+
}
298+
row_total {
299+
value
300+
currency
301+
}
302+
row_total_including_tax {
303+
value
304+
currency
305+
}
306+
}
307+
}
308+
shipping_addresses {
309+
firstname
310+
lastname
311+
company
312+
street
313+
city
314+
postcode
315+
telephone
316+
country {
317+
code
318+
label
319+
}
320+
__typename
265321
}
266322
}
267323
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/AddSimpleProductToCartTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,36 @@ public function testAddSimpleProductToCart()
4545
$response = $this->graphQlMutation($query);
4646
self::assertArrayHasKey('cart', $response['addSimpleProductsToCart']);
4747

48+
self::assertArrayHasKey('shipping_addresses', $response['addSimpleProductsToCart']['cart']);
49+
self::assertEmpty($response['addSimpleProductsToCart']['cart']['shipping_addresses']);
4850
self::assertEquals($quantity, $response['addSimpleProductsToCart']['cart']['items'][0]['quantity']);
4951
self::assertEquals($sku, $response['addSimpleProductsToCart']['cart']['items'][0]['product']['sku']);
52+
self::assertArrayHasKey('prices', $response['addSimpleProductsToCart']['cart']['items'][0]);
53+
54+
self::assertArrayHasKey('price', $response['addSimpleProductsToCart']['cart']['items'][0]['prices']);
55+
$price = $response['addSimpleProductsToCart']['cart']['items'][0]['prices']['price'];
56+
self::assertArrayHasKey('value', $price);
57+
self::assertEquals(10, $price['value']);
58+
self::assertArrayHasKey('currency', $price);
59+
self::assertEquals('USD', $price['currency']);
60+
61+
self::assertArrayHasKey('row_total', $response['addSimpleProductsToCart']['cart']['items'][0]['prices']);
62+
$rowTotal = $response['addSimpleProductsToCart']['cart']['items'][0]['prices']['row_total'];
63+
self::assertArrayHasKey('value', $rowTotal);
64+
self::assertEquals(20, $rowTotal['value']);
65+
self::assertArrayHasKey('currency', $rowTotal);
66+
self::assertEquals('USD', $rowTotal['currency']);
67+
68+
self::assertArrayHasKey(
69+
'row_total_including_tax',
70+
$response['addSimpleProductsToCart']['cart']['items'][0]['prices']
71+
);
72+
$rowTotalIncludingTax =
73+
$response['addSimpleProductsToCart']['cart']['items'][0]['prices']['row_total_including_tax'];
74+
self::assertArrayHasKey('value', $rowTotalIncludingTax);
75+
self::assertEquals(20, $rowTotalIncludingTax['value']);
76+
self::assertArrayHasKey('currency', $rowTotalIncludingTax);
77+
self::assertEquals('USD', $rowTotalIncludingTax['currency']);
5078
}
5179

5280
/**
@@ -231,6 +259,34 @@ private function getQuery(string $maskedQuoteId, string $sku, float $quantity):
231259
product {
232260
sku
233261
}
262+
prices {
263+
price {
264+
value
265+
currency
266+
}
267+
row_total {
268+
value
269+
currency
270+
}
271+
row_total_including_tax {
272+
value
273+
currency
274+
}
275+
}
276+
}
277+
shipping_addresses {
278+
firstname
279+
lastname
280+
company
281+
street
282+
city
283+
postcode
284+
telephone
285+
country {
286+
code
287+
label
288+
}
289+
__typename
234290
}
235291
}
236292
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetAvailableShippingMethodsTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ public function testGetAvailableShippingMethods()
7373
$expectedAddressData,
7474
$response['cart']['shipping_addresses'][0]['available_shipping_methods'][0]
7575
);
76+
self::assertCount(1, $response['cart']['shipping_addresses'][0]['cart_items']);
77+
self::assertCount(1, $response['cart']['shipping_addresses'][0]['cart_items_v2']);
78+
self::assertEquals(
79+
'simple_product',
80+
$response['cart']['shipping_addresses'][0]['cart_items_v2'][0]['product']['sku']
81+
);
7682
}
7783

7884
/**
@@ -140,6 +146,13 @@ private function getQuery(string $maskedQuoteId): string
140146
cart_item_id
141147
quantity
142148
}
149+
cart_items_v2 {
150+
id
151+
quantity
152+
product {
153+
sku
154+
}
155+
}
143156
available_shipping_methods {
144157
amount {
145158
value

0 commit comments

Comments
 (0)