Skip to content

Commit

Permalink
ENGCOM-5576: Add Totals to CartItemInterface #664
Browse files Browse the repository at this point in the history
  • Loading branch information
lenaorobei authored Aug 12, 2019
2 parents 0f2f9b5 + 79b3945 commit bc0026d
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 2 deletions.
76 changes: 76 additions & 0 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/CartItemPrices.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Resolver;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Model\Cart\Totals;
use Magento\Quote\Model\Quote\Item;
use Magento\Quote\Model\Quote\TotalsCollector;

/**
* @inheritdoc
*/
class CartItemPrices implements ResolverInterface
{
/**
* @var TotalsCollector
*/
private $totalsCollector;

/**
* @var Totals
*/
private $totals;

/**
* @param TotalsCollector $totalsCollector
*/
public function __construct(
TotalsCollector $totalsCollector
) {
$this->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(),
],
];
}
}
7 changes: 7 additions & 0 deletions app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,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!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down Expand Up @@ -105,6 +110,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']);
Expand All @@ -128,6 +138,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']);
Expand Down Expand Up @@ -191,9 +206,25 @@ private function getQuery(string $maskedQuoteId): string
return <<<QUERY
{
cart(cart_id: "$maskedQuoteId") {
items {
prices {
price {
value
currency
}
row_total {
value
currency
}
row_total_including_tax {
value
currency
}
}
}
prices {
grand_total {
value,
value
currency
}
subtotal_including_tax {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public function testGetCartTotalsWithTaxApplied()
$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(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']);
Expand Down Expand Up @@ -98,6 +103,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']);
Expand All @@ -120,6 +130,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']);
Expand Down Expand Up @@ -161,9 +176,25 @@ private function getQuery(string $maskedQuoteId): string
return <<<QUERY
{
cart(cart_id: "$maskedQuoteId") {
items {
prices {
price {
value
currency
}
row_total {
value
currency
}
row_total_including_tax {
value
currency
}
}
}
prices {
grand_total {
value,
value
currency
}
subtotal_including_tax {
Expand Down

0 comments on commit bc0026d

Please sign in to comment.