Loading...
How to get cart discount amount in minicart in Magento 2

How to get cart discount amount in minicart in Magento 2

Introduction

Here we will learn about How to get a cart discount amount in a mini-cart in Magento 2.

We have to create a new module if you have already not created one after that follow the steps that describe in the post.

xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  <type name="Magento\Checkout\CustomerData\Cart">
      <plugin name="interceptCustomerDataCart" type="Namespace\Modulename\Plugin\CustomerData\Cart"/>
  </type>
</config>

We need to register the Plugin in Magento 2.

add the following code in “etc/frontend/di.xml” of your module

xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  <type name="Magento\Checkout\CustomerData\Cart">
      <plugin name="interceptCustomerDataCart" type="Namespace\Modulename\Plugin\CustomerData\Cart"/>
  </type>
</config>

Add the following code to your plugin file

Namespace\Modulename\Plugin\CustomerData\Cart.php

<?php

 

namespace Namespace\Modulename\Plugin\CustomerData;

 

use Magento\Checkout\CustomerData\Cart as CustomerDataCart;

 

class Cart
{

   protected $checkoutSession;

   protected $cartTotalRepository;

public function __construct(
       \Magento\Checkout\Model\Session $checkoutSession,
       \Magento\Quote\Model\Cart\CartTotalRepository $cartTotalRepository,
   ) {
       $this->checkoutSession = $checkoutSession;
       $this->cartTotalRepository = $cartTotalRepository;    
   }

/**
    * @param \Magento\Checkout\CustomerData\Cart $subject
    * @param $data
    * @return array
    */
   public function aroundGetSectionData(CustomerDataCart $subject,\Closure $proceed)
   {

    $data = $proceed();

    $totals = $this->cartTotalRepository->get($this->checkoutSession->getQuote()->getId());
    
    $discountAmount = $totals->getDiscountAmount();

       $atts=[
        'cart_discount'=>$discountAmount,
       ];

       return array_merge($data, $atts);

   }

}

Now you need to run the dependency Setup in your console or terminal command

php bin/magento setup:di:compile

Flush the cache in your console or terminal command

php bin/magento cache:clean && bin/magento cache:flush

Contact us:+91 8128405131

Email send us at hello@elightwalk.com

Pravin Prajapati
Full Stack Developer

Expert in frontend and backend development, combining creativity with sharp technical knowledge. Passionate about keeping up with industry trends, he implements cutting-edge technologies, showcasing strong problem-solving skills and attention to detail in crafting innovative solutions.

Most Visited Blog

Blog
How to add customer last logged in column customer grid in Magento 2?

Easily improve client insights with Magento 2! Learn how to add a 'Last Logged In' column to the customer grid with ease. To optimize client management and personalized experience, follow our step-by-step tutorial.

Read More
Blog
How to clear shopping cart in Magento 2

Discover a quick and simple approach to emptying your Magento 2 shopping basket. Streamline your shopping experience by learning how to easily delete things and refresh your basket.

Read More