自定义总计收集器:在结帐中对总计概览中的总计进行重新排序 [英] Custom totals collector: Reorder totals in totals overview in checkout

查看:75
本文介绍了自定义总计收集器:在结帐中对总计概览中的总计进行重新排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义总计收集器,以向合格的客户授予购物车小计3%的折扣.我的收集器的代码如下所示:

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;
    }
    // ...
}

我的问题是要更改总计列表中总计的顺序(例如,在查看购物车时).当前订单是小计,运输,我的折扣,...,总计",但我希望使用小计,我的折扣,运输,...".目前,我的config.xml看起来像这样:

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>

我为"before"元素和"after"元素尝试了不同的设置,但这并没有影响总计的列出顺序,它只影响了grand_total的计算.这很奇怪,但是我的总数仅包含在具有上述设置的grand_total的计算中.例如,如果我将之后"设置为小计",或者如果将之前"设置为小计",那么我的总数完全不会影响大计的计算.也许有人可以向我解释.

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.

那么,如何更改总计的顺序?当我将"after"设置为除"shipping"以外的任何内容时,为什么结果如此怪异?我是否误解了这两个配置元素的功能?

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?

推荐答案

您上面的XML代码段用于总计收集器在后端触发的顺序.前端顺序在另一个位置控制.在Mage/Core/Sales/etc中的config.xml中:

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>

如果将<my_discount>作为节点添加到模块config.xml文件中的<totals_sort>下,则可以将其插入任意位置.

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

这篇关于自定义总计收集器:在结帐中对总计概览中的总计进行重新排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆