简体   繁体   中英

Adding A Custom Discount Order Total in Magento Does Not Change Sales Tax

I have created a custom order total that gives a discount in certain situations. The grand total always comes out correct, however the sales tax calculation is not taking my discount into account when calculating (so if I was giving a discount of $10, the sales tax amount was calculated on the entire amount before my discount).

Take for example the following:

Subtotal:              $856.49
Multi Unit Discounts: -$22.50
Shipping:              $10.96
Tax:                   $52.05
Grand Total:           $897.00

My custom discount is the Multi Unit Discounts. The tax rate is 6%. As you can see the grand total is correct based on all the line items, but the tax amount itself is not correct (it is based on all the line items except my discount).

In my config.xml file I have the following to get my order total working in the system:

     <sales>
        <quote>
            <totals>
                <mud>
                    <class>Wpe_Multiunitdiscount_Model_Multiunitdiscount</class>
                    <before>tax</before>
                </mud>
            </totals>
        </quote>
    </sales>    

The following is the contents of my order total class:

class Wpe_Multiunitdiscount_Model_Multiunitdiscount extends Mage_Sales_Model_Quote_Address_Total_Abstract {

public function collect(Mage_Sales_Model_Quote_Address $address) {

    if ($address->getData('address_type')=='billing') return $this;

    $items = $address->getAllItems();

    $total_discount = 0;

    foreach($items as $item) {
        $product_discounts = Mage::helper("multiunitdiscount")->findDiscounts($item);
        if($product_discounts > 0) {
            $total_discount += $product_discounts;
        }
    }

    $address->setMudAmount($total_discount);

    $address->setGrandTotal($address->getGrandTotal() - $address->getMudAmount() );
$address->setBaseGrandTotal($address->getBaseGrandTotal() - $address->getMudAmount());
    return $this;
}

public function fetch(Mage_Sales_Model_Quote_Address $address) {

    if ($address->getData('address_type')=='billing') return $this;

    if($address->getMudAmount() > 0) {

        $address->addTotal(array(
            'code'  => $this->getCode(),
            'title' => Mage::helper('sales')->__('Multi Unit Discounts'),
            'value' => -$address->getMudAmount(),
        ));
    }
    return $this;
}

}

For the sake of not posting a huge chunk of code in here that I am not sure is necessary, I can tell you that the helper in the above code simply returns the amount of money the discount is for that particular item in the quote.

Can someone help point me in the right direction for getting the sales tax calculation correct?

EDIT:

In order to keep this simple, I have removed a lot of my logic behind calculating the discount and am now trying to simple take $10 off the order total as a discount. As suggested I did not modify the Grand Total of the address and am now only setting the Discount Amount and Base Discount Amount. Now the sales tax does not add up and the grand total is off. Maybe if there is a good tutorial out there that someone can point me towards would help? I do not seem to be grasping how the order totals all interact with each other.

public function collect(Mage_Sales_Model_Quote_Address $address) {

    if ($address->getData('address_type')=='billing') return $this;

    $address->setMudDiscount(10);
    $address->setDiscountAmount($address->getDiscountAmount() + $address->getMudDiscount());
    $address->setBaseDiscountAmount($address->getBaseDiscountAmount() + $address->getMudDiscount());

    return $this;
}

public function fetch(Mage_Sales_Model_Quote_Address $address) {

    if ($address->getData('address_type')=='billing') return $this;

    $address->addTotal(array(
        'code'  => $this->getCode(),
        'title' => Mage::helper('sales')->__('Multi Unit Discounts'),
        'value' => -$address->getMudDiscount(),
    ));
    return $this;
}

Go to System > Configuration . Select " Tax " from the left hand navigation, then open the "Calculation Settings" group if it isn't already.

Try changing the " Apply Customer Tax " parameter to " After Discount "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM