This repository has been archived by the owner on Dec 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathCartPrices.php
106 lines (95 loc) · 3.17 KB
/
CartPrices.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?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\Quote;
use Magento\Quote\Model\Quote\Address\Total;
use Magento\Quote\Model\Quote\TotalsCollector;
/**
* @inheritdoc
*/
class CartPrices implements ResolverInterface
{
/**
* @var TotalsCollector
*/
private $totalsCollector;
/**
* @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 Quote $quote */
$quote = $value['model'];
$cartTotals = $this->totalsCollector->collectQuoteTotals($quote);
$currency = $quote->getQuoteCurrencyCode();
return [
'grand_total' => ['value' => $cartTotals->getGrandTotal(), 'currency' => $currency],
'subtotal_including_tax' => ['value' => $cartTotals->getSubtotalInclTax(), 'currency' => $currency],
'subtotal_excluding_tax' => ['value' => $cartTotals->getSubtotal(), 'currency' => $currency],
'subtotal_with_discount_excluding_tax' => [
'value' => $cartTotals->getSubtotalWithDiscount(), 'currency' => $currency
],
'applied_taxes' => $this->getAppliedTaxes($cartTotals, $currency),
'discount' => $this->getDiscount($cartTotals, $currency),
'model' => $quote
];
}
/**
* Returns taxes applied to the current quote
*
* @param Total $total
* @param string $currency
* @return array
*/
private function getAppliedTaxes(Total $total, string $currency): array
{
$appliedTaxesData = [];
$appliedTaxes = $total->getAppliedTaxes();
if (count($appliedTaxes) === 0) {
return $appliedTaxesData;
}
foreach ($appliedTaxes as $appliedTax) {
$appliedTaxesData[] = [
'label' => $appliedTax['id'],
'amount' => ['value' => $appliedTax['amount'], 'currency' => $currency]
];
}
return $appliedTaxesData;
}
/**
* Returns information about an applied discount
*
* @param Total $total
* @param string $currency
* @return array|null
*/
private function getDiscount(Total $total, string $currency)
{
if ($total->getDiscountAmount() === 0) {
return null;
}
return [
'label' => explode(', ', $total->getDiscountDescription()),
'amount' => ['value' => $total->getDiscountAmount(), 'currency' => $currency]
];
}
}