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.
We use cookies to make your experience better. To comply with the new e-Privacy directive, we need to ask for your consent to set the cookies. Privacy Policy.
Take Your Business Global With Our Premium Solutions
Our website development team has experience in various industry sectors, such as Fashion, Jewelry, Electronics, Furniture, Fintech, Travel, and Health. We help develop user-friendly websites that represent your brand's value on the internet and drive more sales. Our solutions are built for performance, security, and a smooth user experience. Trust our expert team to deliver innovative, scalable web development solutions designed to meet the needs of your industry and drive your business to new heights.
We Build Your Identity to Present to the World
Elightwalk is a 15+ year-experienced global e-commerce development firm that offers a wide range of business solutions. We collaborate with clients of all sizes and shapes worldwide to create excellent digital products that many users use. Our 75+ innovations pave the way for experienced new technologies for our developers. You can rely on our talented team of certified e-commerce and application developers.
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 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 in “etc/frontend/di.xml” of your module
<?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>
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);
}
}
php bin/magento setup:di:compile
php bin/magento cache:clean && bin/magento cache:flush
Contact us:+91 8128405131
Email send us at hello@elightwalk.com
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.
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.
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.