Magento Add Fee or Discount to Order Totals

Source: Internet
Author: User

In this tutorial, we'll see how to add New line item to Magento order totals.

What's this means was, how to add a additional Fee or Discount, or any kind of charge to order total of the Magento Che Ckout process.
In a typical order, the order totals usually comprises of Sub total, Shipping cost, Taxes, Discount, based on these values The total order grand total is calculated. Now if we want to add a additional credit Card Fee or convince free or Affiliate Discount or any other order total which Would affect the order grand total we need to create a Magento module. This extra fee which we is adding to the total would reflect in the

    • Checkout Page Order Total
    • Cart Page Order Total
    • My account Order View Page
    • Print Order PDF
    • Order EMails
    • Admin Order View/email/pdf
    • Admin Invoice View/email/pdf
    • Admin Credit Memo View/email/pdf

As can see based on the above list this module is not being simple. In this tutorial, I am attaching the source of such a very basic module which you can use a your starting point to add an Extra change. I would also explain the basics of how to implement this. In this tutorial I would add a new order total called ' Fee ' with a fixed cost of 10$.
P.s:this Module only applies Fee to the order when order was placed from Frontend. If you create a order admin, this module is not tested for the case. Also This module was tested in Magento version 1.6, but should work on 1.4-1.7
Here is few screenshots of the module

Admin Order View Page

Order Email

Checkout Page Order Totals

Error ... Unable to load download template. Search Single-download-template.tpl in your plugin folder!
Before starting with explanation, this is quite a advanced and big tutorial so it would being difficult to explain all thing s in details. I'll just put in the basic stuff here, rest of need to debug from the source code itself.

Checkout Page Total Order Total Basics

We'll see how to add the totals only to the checkout page. All the totals line items, show up the checkout page come from files located at folder mage\sales\model\quote\address\ Total. In Magento before order was placed all order data are stored in a quote object and after order is placed it gets transferred to the Order object. The quote totals follow the collector pattern and we can add collector as many collector classes. To add collector to the quote object in we config. We add the lines

<global>    <sales>            <quote>                <totals>                    <fee>                        <class>fee/ sales_quote_address_total_fee</class>                    </fee>                </totals>            </quote>       </ Sales></global>

This means whenever the totals is calculated for a quote, it'll also call this class. The all collectors is called from the Collecttotals () function in the Quote Model.
In our collector class we put in the code

<?phpclass Excellence_fee_model_sales_quote_address_total_fee extends Mage_sales_model_quote_address_total_ abstract{protected $_code = ' fee ';p ublic function collect (mage_sales_model_quote_address $address) {parent::collect ($ Address), $this->_setamount (0), $this->_setbaseamount (0), $items = $this->_getaddressitems ($address); Count ($items)) {return $this;//this makes only address type shipping to come through} $quote = $address->getquote (); if ( Excellence_fee_model_fee::canapply ($address)) {//your Business Logic$exist_amount = $quote->getfeeamount (); $fee = Excellence_fee_model_fee::getfee (); $balance = $fee-$exist _amount; $address->setfeeamount ($balance); $address- >setbasefeeamount ($balance), $quote->setfeeamount ($balance), $address->setgrandtotal ($address, Getgrandtotal () + $address->getfeeamount ()), $address->setbasegrandtotal ($address->getbasegrandtotal () + $ Address->getbasefeeamount ());}} Public function fetch (mage_sales_model_quote_address $addrESS) {$amt = $address->getfeeamount (); $address->addtotal (' Code ' = $this->getcode (), ' title ' = Mage::helper (' fee ')->__ (' fee '), ' value ' = $amt)); return $this;}}

The both main functions here is collect () and fetch (). In collect function you add whatever amount your want to the order totals, and fetch () are used for display purposes. If This is do properly, you should see your order all line in the checkout and cart page.
Here we are using the Fee_amount and Base_fee_amount, which contain our fee amount. We'll have a to see save these and a to database and so in our module installer file we add this code

ALTER TABLE  ' ". $this->gettable (' sales/quote_address ')." ' ADD  ' Fee_amount ' DECIMAL (ten, 2) not NULL; ALTER TABLE  ' ". $this->gettable (' sales/quote_address ')." ' ADD  ' Base_fee_amount ' DECIMAL (ten, 2) not NULL;
Order Page

Till now, all code written have been done with the for the quote object. But after order was placed, we need to transfer all information to the Order object. As you would has seen above we are using the fields Fee_amount and Base_fee_amount, we now need to store these The order table as well. To does all the above we need to do the things. First in the config file add this code inside the Global tab,

<fieldsets>            <sales_convert_quote_address>                <fee_amount><to_order>*</to_order ></fee_amount>                <base_fee_amount><to_order>*</to_order></base_fee_amount>            </sales_convert_quote_address>        </fieldsets>

And in our module install file

ALTER TABLE  ' ". $this->gettable (' Sales/order ')." ' ADD  ' Fee_amount ' DECIMAL (ten, 2) not NULL; ALTER TABLE  ' ". $this->gettable (' Sales/order ')." ' ADD  ' Base_fee_amount ' DECIMAL (ten, 2) not NULL;

After doing this, these, should get saved to the order table from the quote table.

This was only basics of the adding a line item to order total. Rest There is lot of code written inside the attached module, please go through it in detail to understand more.

Magento Add Fee or Discount to Order Totals

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.