From c8e30b870450c40e8eca1058f39646d23d5d3480 Mon Sep 17 00:00:00 2001 From: Patrick McLain Date: Thu, 2 May 2019 21:05:44 -0400 Subject: [PATCH] Add Totals to CartItemInterface Fixes magento/graphql-ce#633 --- .../Model/Resolver/CartItemPrices.php | 76 +++++++++++++++++++ .../Magento/QuoteGraphQl/etc/schema.graphqls | 7 ++ .../GraphQl/Quote/Customer/CartTotalsTest.php | 33 +++++++- .../GraphQl/Quote/Guest/CartTotalsTest.php | 33 +++++++- 4 files changed, 147 insertions(+), 2 deletions(-) create mode 100644 app/code/Magento/QuoteGraphQl/Model/Resolver/CartItemPrices.php diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/CartItemPrices.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/CartItemPrices.php new file mode 100644 index 0000000000000..a591c74e78db3 --- /dev/null +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/CartItemPrices.php @@ -0,0 +1,76 @@ +totalsCollector = $totalsCollector; + } + + /** + * @inheritdoc + */ + public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) + { + if (!isset($value['model'])) { + throw new LocalizedException(__('"model" value should be specified')); + } + /** @var Item $cartItem */ + $cartItem = $value['model']; + + if (!$this->totals) { + // The totals calculation is based on quote address. + // But the totals should be calculated even if no address is set + $this->totals = $this->totalsCollector->collectQuoteTotals($cartItem->getQuote()); + } + + $currencyCode = $cartItem->getQuote()->getQuoteCurrencyCode(); + + return [ + 'price' => [ + 'currency' => $currencyCode, + 'value' => $cartItem->getPrice(), + ], + 'row_total' => [ + 'currency' => $currencyCode, + 'value' => $cartItem->getRowTotal(), + ], + 'row_total_including_tax' => [ + 'currency' => $currencyCode, + 'value' => $cartItem->getRowTotalInclTax(), + ], + ]; + } +} diff --git a/app/code/Magento/QuoteGraphQl/etc/schema.graphqls b/app/code/Magento/QuoteGraphQl/etc/schema.graphqls index e2d29e93db574..0e732e6cf247e 100644 --- a/app/code/Magento/QuoteGraphQl/etc/schema.graphqls +++ b/app/code/Magento/QuoteGraphQl/etc/schema.graphqls @@ -308,9 +308,16 @@ type VirtualCartItem implements CartItemInterface @doc(description: "Virtual Car interface CartItemInterface @typeResolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\CartItemTypeResolver") { id: String! quantity: Float! + prices: CartItemPrices @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartItemPrices") product: ProductInterface! } +type CartItemPrices { + price: Money! + row_total: Money! + row_total_including_tax: Money! +} + type SelectedCustomizableOption { id: Int! label: String! diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartTotalsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartTotalsTest.php index bb8acfce629ff..e214cac4f2bd3 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartTotalsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CartTotalsTest.php @@ -50,6 +50,11 @@ public function testGetCartTotalsWithTaxApplied() $query = $this->getQuery($maskedQuoteId); $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); + $cartItem = $response['cart']['items'][0]; + self::assertEquals(10, $cartItem['prices']['price']['value']); + self::assertEquals(20, $cartItem['prices']['row_total']['value']); + self::assertEquals(21.5, $cartItem['prices']['row_total_including_tax']['value']); + self::assertArrayHasKey('prices', $response['cart']); $pricesResponse = $response['cart']['prices']; self::assertEquals(21.5, $pricesResponse['grand_total']['value']); @@ -78,6 +83,11 @@ public function testGetTotalsWithNoTaxApplied() $query = $this->getQuery($maskedQuoteId); $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); + $cartItem = $response['cart']['items'][0]; + self::assertEquals(10, $cartItem['prices']['price']['value']); + self::assertEquals(20, $cartItem['prices']['row_total']['value']); + self::assertEquals(20, $cartItem['prices']['row_total_including_tax']['value']); + $pricesResponse = $response['cart']['prices']; self::assertEquals(20, $pricesResponse['grand_total']['value']); self::assertEquals(20, $pricesResponse['subtotal_including_tax']['value']); @@ -101,6 +111,11 @@ public function testGetCartTotalsWithNoAddressSet() $query = $this->getQuery($maskedQuoteId); $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); + $cartItem = $response['cart']['items'][0]; + self::assertEquals(10, $cartItem['prices']['price']['value']); + self::assertEquals(20, $cartItem['prices']['row_total']['value']); + self::assertEquals(20, $cartItem['prices']['row_total_including_tax']['value']); + $pricesResponse = $response['cart']['prices']; self::assertEquals(20, $pricesResponse['grand_total']['value']); self::assertEquals(20, $pricesResponse['subtotal_including_tax']['value']); @@ -164,9 +179,25 @@ private function getQuery(string $maskedQuoteId): string return <<getQuery($maskedQuoteId); $response = $this->graphQlQuery($query); + $cartItem = $response['cart']['items'][0]; + self::assertEquals(10, $cartItem['prices']['price']['value']); + self::assertEquals(20, $cartItem['prices']['row_total']['value']); + self::assertEquals(21.5, $cartItem['prices']['row_total_including_tax']['value']); + self::assertArrayHasKey('prices', $response['cart']); $pricesResponse = $response['cart']['prices']; self::assertEquals(21.5, $pricesResponse['grand_total']['value']); @@ -72,6 +77,11 @@ public function testGetTotalsWithNoTaxApplied() $query = $this->getQuery($maskedQuoteId); $response = $this->graphQlQuery($query); + $cartItem = $response['cart']['items'][0]; + self::assertEquals(10, $cartItem['prices']['price']['value']); + self::assertEquals(20, $cartItem['prices']['row_total']['value']); + self::assertEquals(20, $cartItem['prices']['row_total_including_tax']['value']); + $pricesResponse = $response['cart']['prices']; self::assertEquals(20, $pricesResponse['grand_total']['value']); self::assertEquals(20, $pricesResponse['subtotal_including_tax']['value']); @@ -95,6 +105,11 @@ public function testGetCartTotalsWithNoAddressSet() $query = $this->getQuery($maskedQuoteId); $response = $this->graphQlQuery($query); + $cartItem = $response['cart']['items'][0]; + self::assertEquals(10, $cartItem['prices']['price']['value']); + self::assertEquals(20, $cartItem['prices']['row_total']['value']); + self::assertEquals(20, $cartItem['prices']['row_total_including_tax']['value']); + $pricesResponse = $response['cart']['prices']; self::assertEquals(20, $pricesResponse['grand_total']['value']); self::assertEquals(20, $pricesResponse['subtotal_including_tax']['value']); @@ -136,9 +151,25 @@ private function getQuery(string $maskedQuoteId): string return <<