Question

I have created a custom totals collector to grant qualified customers a discount of 3% of the cart's subtotal. The code of my collector looks like the following:

class My_Module_Model_DiscountCollector
    extends Mage_Sales_Model_Quote_Address_Total_Abstract
{
    // ...
    public function collect(Mage_Sales_Model_Quote_Address $address)
    {
        if($this->userIsQualified())
        {
            parent::collect($address);

            // $this->_inclTax tells the collector to either calculate the actual discount amount 
            // based on the subtotal including or excluding tax
            $baseCalcValue = ($this->_inclTax) ? $address->getBaseSubtotalTotalInclTax() : $address->getBaseSubtotal();
            $calcValue = ($this->_inclTax) ? $address->getSubtotalInclTax() : $address->getSubtotal();

            $baseDiscountAmount = $baseCalcValue * 0.03;
            $discountAmount = $calcValue * 0.03;

            $this->_setBaseAmount(-$baseDiscountAmount);
            $this->_setAmount(-$discountAmount);
        }
        return $this;
    }
    public function fetch(Mage_Sales_Model_Quote_Address $address)
    {
        if($this->userIsQualified())
        {
            $discountAmount = (($this->_inclTax) ? $address->getSubtotalInclTax() : $address->getSubtotal()) * 0.03;
            $address->addTotal(
                array(
                    "code"  => $this->getCode(),
                    "title" => "My Discount (3%)",
                    "value" => -$discountAmount
                )
            );
        }
        return $this;
    }
    // ...
}

My problem is to change the order of the totals in the totals listing (for example when viewing the cart). The current order is "Subtotal, Shipping, My Discount, ..., Grand Total", but I would prefer "Subtotal, My Discount, Shipping, ...". Currently my config.xml looks something like this:

<config>
    <!-- ... --->
    <global>
        <!-- ... -->
        <sales>
            <quote>
                <totals>
                    <my_discount>
                        <class>My_Module_Model_DiscountCollector</class
                        <after>shipping</after>
                        <!--<before>grand_total</before>-->
                        <!--<after>shipping</after>-->
                        <!--<before>shipping</before>-->
                    </my_discount>
                </totals>
            </quote>
        </sales>
        <!-- ... -->
    </global>
</config>

I tried different settings for the "before"- and "after"-elements, but that didn't affect the order the totals are listed in, it only affected the calculation of the grand_total. It's weird, but my total is only included in the calculation of the grand_total with the settings above. For example, if I set "after" to "subtotal" or if I set "before" to "grand_total", my total doesn't affect the calculation of the grand_total at all. Maybe someone can explain that to me.

So how do I change the order of the totals? Why are the results so weird when I set "after" to anything else but "shipping"? Have I misunderstood the function of those two config-elements?

Was it helpful?

Solution

The XML snippet you have above is for the order that the totals collectors fire on the backend. The frontend order is controlled in a different place. From config.xml in Mage/Core/Sales/etc:

...
<default>
...
    <sales>
        <totals_sort>
            <discount>20</discount>
            <grand_total>100</grand_total>
            <shipping>30</shipping>
            <subtotal>10</subtotal>
            <tax>40</tax>
        </totals_sort>
...
</default>

If you add <my_discount> as a node under <totals_sort> in your module config.xml file you can insert it where you like.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top