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.
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
Table of contents
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.