diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/CartPrices.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/CartPrices.php index 6a57a7662af09..52a88f9468e37 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Resolver/CartPrices.php +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/CartPrices.php @@ -33,6 +33,27 @@ public function __construct( ) { $this->totalsCollector = $totalsCollector; } + public function getDiscountValue (Quote $quote){ + $address = $quote->getShippingAddress(); + $totalDiscounts = $address->getExtensionAttributes()->getDiscounts(); + if ($totalDiscounts && is_array($totalDiscounts)) { + foreach ($totalDiscounts as $value) { + $discount = []; + $amount = []; + $discount['label'] = $value->getRuleLabel() ?: __('Discount'); + /* @var \Magento\SalesRule\Api\Data\DiscountDataInterface $discountData */ + $discountData = $value->getDiscountData(); + $amount['value'] = $discountData->getAmount(); + $amount['currency'] = $quote->getQuoteCurrencyCode(); + $discount['amount'] = $amount; + $discountValues[] = $discount; + } + foreach ($discountValues as $individualDiscountValue) { + return $individualDiscountValue['amount']['value']; + } + } + return 0; + } /** * @inheritdoc @@ -47,9 +68,11 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value $quote = $value['model']; $cartTotals = $this->totalsCollector->collectQuoteTotals($quote); $currency = $quote->getQuoteCurrencyCode(); + $discountValue = $this->getDiscountValue($quote); + $grandTotal = $cartTotals->getGrandTotal() - $discountValue; return [ - 'grand_total' => ['value' => $cartTotals->getGrandTotal(), 'currency' => $currency], + 'grand_total' => ['value' => $grandTotal, 'currency' => $currency], 'subtotal_including_tax' => ['value' => $cartTotals->getSubtotalInclTax(), 'currency' => $currency], 'subtotal_excluding_tax' => ['value' => $cartTotals->getSubtotal(), 'currency' => $currency], 'subtotal_with_discount_excluding_tax' => [