Frage

So I have a custom discount applied the following way:

on vendor\module\etc\sales.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Sales:etc/sales.xsd">
    <section name="quote">
        <group name="totals">
            <item name="testdiscount" instance="vendor\module\Model\Quote\Discount" sort_order="500"/>
        </group>
    </section>
</config>

On my vendor\module\Model\Quote\Discount.php

class Discount extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
{
    public function __construct(
        \Magento\Framework\Event\ManagerInterface $eventManager,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\SalesRule\Model\Validator $validator,
        \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
    ) 
    {
        $this->setCode('testdiscount');
        $this->eventManager  = $eventManager;
        $this->calculator    = $validator;
        $this->storeManager  = $storeManager;
        $this->priceCurrency = $priceCurrency;
    }

    public function collect(
        \Magento\Quote\Model\Quote $quote,
        \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
        \Magento\Quote\Model\Quote\Address\Total $total
    )
    {

    parent::collect($quote, $shippingAssignment, $total);

    $totalDiscount = //Get discount here
    if($totalDiscount > 0)
    {  
        $total->setDiscountAmount(          - $totalDiscount);
        $total->setBaseDiscountAmount(      - $totalDiscount);
        $total->setSubtotalWithDiscount($total->getSubtotal()         - $totalDiscount);
        $total->setBaseSubtotalWithDiscount($total->getBaseSubtotal() - $totalDiscount);

        $total->addTotalAmount($this->getCode(),     - $totalDiscount);
        $total->addBaseTotalAmount($this->getCode(), - $totalDiscount);

    }

    return $this;

    }
}

My problem is that the taxes are being calculated using the total cart value without the discount applied. Any help on how to make the tax prices being applied to the total price including the discount?

War es hilfreich?

Lösung

The problem was I was only setting the discount for the totals and not for every item in the order:

foreach ($quote->getAllVisibleItems() as $item) 
{
   $item->setDiscountAmount(       $discount);
   $item->setBaseDiscountAmount(   $basedicount);
   $item->setDiscountPercent(      $percDiscount);
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit magento.stackexchange
scroll top