I tried to implement observer for this. Maybe it help to someone.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_quote_address_collect_totals_after">
<observer name="vendor_module_fix_total_discount_amount" instance="Vendor\Module\Observer\FixTotalDiscountAmount" />
</event>
</config>
and its implementation
<?php
namespace Vendor\Module\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\Quote\Address\Total;
use Magento\Tax\Model\Config as TaxConfig;
class FixTotalDiscountAmount implements ObserverInterface
{
private TaxConfig $taxConfig;
private PriceCurrencyInterface $priceCurrency;
public function __construct(TaxConfig $taxConfig, PriceCurrencyInterface $priceCurrency)
{
$this->taxConfig = $taxConfig;
$this->priceCurrency = $priceCurrency;
}
public function execute(Observer $observer)
{
/** @var Quote $quote */
$quote = $observer->getData('quote');
/** @var Total $total */
$total = $observer->getData('total');
if (!$this->taxConfig->displayCartPricesInclTax()) {
// Don't need to add tax into discount.
return;
}
if (
$this->taxConfig->applyTaxAfterDiscount() // Apply Tax After Discount
&& !$this->taxConfig->discountTax() // Apply Discount on Prices Excluding Tax
&& !$this->taxConfig->priceIncludesTax() // Catalog Prices Excluding Tax
) {
// Add "missing" Discount Tax from all Items
foreach ($quote->getAllVisibleItems() as $item) {
$discountTax = $item->getRowTotalInclTax() - $item->getRowTotal() - $item->getTaxAmount();
$baseDiscountTax = $item->getBaseRowTotalInclTax() - $item->getBaseRowTotal() - $item->getBaseTaxAmount();
$discountAmount = $total->getDiscountAmount() - $discountTax; // minus because discounts are negative numbers
$baseDiscountAmount = $total->getBaseDiscountAmount() - $baseDiscountTax;
$total->setDiscountAmount($this->priceCurrency->roundPrice($discountAmount));
$total->setBaseDiscountAmount($this->priceCurrency->roundPrice($baseDiscountAmount));
}
}
}
}