11
11
use Magento \Framework \GraphQl \Config \Element \Field ;
12
12
use Magento \Framework \GraphQl \Query \ResolverInterface ;
13
13
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 ;
15
16
16
17
/**
17
18
* @inheritdoc
18
19
*/
19
20
class Totals implements ResolverInterface
20
21
{
21
22
/**
22
- * @var CartTotalRepositoryInterface
23
+ * @var TotalsCollector
23
24
*/
24
- private $ cartTotalRepository ;
25
+ private $ totalsCollector ;
25
26
26
27
/**
27
- * @param CartTotalRepositoryInterface $cartTotalRepository
28
+ * @param TotalsCollector $totalsCollector
28
29
*/
29
30
public function __construct (
30
- CartTotalRepositoryInterface $ cartTotalRepository
31
+ TotalsCollector $ totalsCollector
31
32
) {
32
- $ this ->cartTotalRepository = $ cartTotalRepository ;
33
+ $ this ->totalsCollector = $ totalsCollector ;
33
34
}
34
35
35
36
/**
@@ -41,34 +42,45 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
41
42
throw new LocalizedException (__ ('"model" value should be specified ' ));
42
43
}
43
44
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 ();
45
49
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
+ ];
57
60
}
58
61
59
62
/**
60
- * Adds currency code to the totals
63
+ * Returns taxes applied to the current quote
61
64
*
62
- * @param array $totals
63
- * @param string|null $currencyCode
65
+ * @param Total $total
66
+ * @param string $currency
64
67
* @return array
65
68
*/
66
- private function addCurrencyCode ( array $ totals , $ currencyCode ): array
69
+ private function getAppliedTaxes ( Total $ total , string $ currency ): array
67
70
{
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
+ ];
70
82
}
71
83
72
- return $ totals ;
84
+ return $ appliedTaxesData ;
73
85
}
74
86
}
0 commit comments