From 592654bc95ba00e14da1539d7d12a879b7fe01e7 Mon Sep 17 00:00:00 2001 From: Sathiya Prakash Date: Thu, 7 Jan 2021 11:28:49 +0530 Subject: [PATCH] Added logic for deducting the discount amount from the total --- .../Model/Resolver/CartPrices.php | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) 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' => [