Skip to content

Commit 8210d82

Browse files
committed
Use a single resolver for totals/taxes for performance improvement
1 parent 8ef99e0 commit 8210d82

File tree

5 files changed

+40
-93
lines changed

5 files changed

+40
-93
lines changed

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

Lines changed: 0 additions & 65 deletions
This file was deleted.

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

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,26 @@
1111
use Magento\Framework\GraphQl\Config\Element\Field;
1212
use Magento\Framework\GraphQl\Query\ResolverInterface;
1313
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
14-
use Magento\Quote\Api\CartTotalRepositoryInterface;
14+
use Magento\Quote\Model\Quote\Address\Total;
15+
use Magento\Quote\Model\Quote\TotalsCollector;
1516

1617
/**
1718
* @inheritdoc
1819
*/
1920
class Totals implements ResolverInterface
2021
{
2122
/**
22-
* @var CartTotalRepositoryInterface
23+
* @var TotalsCollector
2324
*/
24-
private $cartTotalRepository;
25+
private $totalsCollector;
2526

2627
/**
27-
* @param CartTotalRepositoryInterface $cartTotalRepository
28+
* @param TotalsCollector $totalsCollector
2829
*/
2930
public function __construct(
30-
CartTotalRepositoryInterface $cartTotalRepository
31+
TotalsCollector $totalsCollector
3132
) {
32-
$this->cartTotalRepository = $cartTotalRepository;
33+
$this->totalsCollector = $totalsCollector;
3334
}
3435

3536
/**
@@ -41,34 +42,45 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
4142
throw new LocalizedException(__('"model" value should be specified'));
4243
}
4344

44-
$cartTotals = $this->cartTotalRepository->get($value['model']->getId());
45+
/** @var Quote $quote */
46+
$quote = $value['model'];
47+
$cartTotals = $this->totalsCollector->collectQuoteTotals($quote);
48+
$currency = $quote->getQuoteCurrencyCode();
4549

46-
$currency = $cartTotals->getQuoteCurrencyCode();
47-
$data = $this->addCurrencyCode([
48-
'grand_total' => ['value' => $cartTotals->getGrandTotal(), ],
49-
'subtotal_including_tax' => ['value' => $cartTotals->getSubtotalInclTax()],
50-
'subtotal_excluding_tax' => ['value' => $cartTotals->getSubtotal()],
51-
'subtotal_with_discount_excluding_tax' => ['value' => $cartTotals->getSubtotalWithDiscount()]
52-
], $currency);
53-
54-
$data['model'] = $value['model'];
55-
56-
return $data;
50+
return [
51+
'grand_total' => ['value' => $cartTotals->getGrandTotal(), 'currency' => $currency],
52+
'subtotal_including_tax' => ['value' => $cartTotals->getSubtotalInclTax(), 'currency' => $currency],
53+
'subtotal_excluding_tax' => ['value' => $cartTotals->getSubtotal(), 'currency' => $currency],
54+
'subtotal_with_discount_excluding_tax' => [
55+
'value' => $cartTotals->getSubtotalWithDiscount(), 'currency' => $currency
56+
],
57+
'applied_taxes' => $this->getAppliedTaxes($cartTotals, $currency),
58+
'model' => $quote
59+
];
5760
}
5861

5962
/**
60-
* Adds currency code to the totals
63+
* Returns taxes applied to the current quote
6164
*
62-
* @param array $totals
63-
* @param string|null $currencyCode
65+
* @param Total $total
66+
* @param string $currency
6467
* @return array
6568
*/
66-
private function addCurrencyCode(array $totals, $currencyCode): array
69+
private function getAppliedTaxes(Total $total, string $currency): array
6770
{
68-
foreach ($totals as &$total) {
69-
$total['currency'] = $currencyCode;
71+
$appliedTaxes = $total->getAppliedTaxes();
72+
73+
if (count($appliedTaxes) === 0) {
74+
return [];
75+
}
76+
77+
foreach ($appliedTaxes as $appliedTax) {
78+
$appliedTaxesData[] = [
79+
'label' => $appliedTax['id'],
80+
'amount' => ['value' => $appliedTax['amount'], 'currency' => $currency]
81+
];
7082
}
7183

72-
return $totals;
84+
return $appliedTaxesData;
7385
}
7486
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ type CartPrices {
125125
subtotal_including_tax: Money
126126
subtotal_excluding_tax: Money
127127
subtotal_with_discount_excluding_tax: Money
128-
applied_taxes: [CartTaxItem] @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartTaxes")
128+
applied_taxes: [CartTaxItem]
129129
}
130130

131131
type CartTaxItem {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function testGetCartTotalsForCustomerWithTaxApplied()
9090

9191
self::assertArrayHasKey('prices', $response['cart']);
9292
$pricesResponse = $response['cart']['prices'];
93-
self::assertEquals(10, $pricesResponse['grand_total']['value']);
93+
self::assertEquals(10.83, $pricesResponse['grand_total']['value']);
9494
self::assertEquals(10.83, $pricesResponse['subtotal_including_tax']['value']);
9595
self::assertEquals(10, $pricesResponse['subtotal_excluding_tax']['value']);
9696
self::assertEquals(10, $pricesResponse['subtotal_with_discount_excluding_tax']['value']);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function testGetCartTotalsForCustomerWithTaxApplied()
9090

9191
self::assertArrayHasKey('prices', $response['cart']);
9292
$pricesResponse = $response['cart']['prices'];
93-
self::assertEquals(10, $pricesResponse['grand_total']['value']);
93+
self::assertEquals(10.83, $pricesResponse['grand_total']['value']);
9494
self::assertEquals(10.83, $pricesResponse['subtotal_including_tax']['value']);
9595
self::assertEquals(10, $pricesResponse['subtotal_excluding_tax']['value']);
9696
self::assertEquals(10, $pricesResponse['subtotal_with_discount_excluding_tax']['value']);

0 commit comments

Comments
 (0)