Home » Magento tutorials » How to Add fee or discount to order totals in Magento

How to Add fee or discount to order totals in Magento

In this tutorial, we will see how to add new line item to magento order totals. What this means is that, how to add an additional Fee or Discount, or any kind of charge to order total of the magento checkout 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 an additional Credit Card Fee or Convince Free or Affiliate Discount or any other order total which will affect the order grand total we need to create a magento module. This extra fee which we are 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 you can see based on the above list this module is not be 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 will 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 is placed from frontend. If you create an order admin, this module is not tested for that case. Also this module is tested in magento version 1.6, but should work on 1.4-1.7

Here are few screenshots of the module

Admin Order View Page

Admin Order View Page

Order Email

Order Email

Checkout Page Order Totals

Checkout Page Order Totals

You can download the file from here:
Download: Order Totals Module(17.03KB)

Before starting with explanation, this is quite an advanced and big tutorial so it would be difficult to explain all things in details. I will just put in the basic stuff here, rest you need to debug from the source code itself.

Checkout Page Total Order Total Basics

We will see how to add the totals only to the checkout page. All the totals line items that show up the checkout page come from files located at folder Mage\Sales\Model\Quote\Address\Total. In magento before order is placed all order data is 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 our config.xml we add the lines

This means whenever the totals are calculated for a quote, it will also call this class. All collectors are called from the collectTotals() function in the Quote Model.

In our collector class we put in the code

The two main functions here are collect() and fetch(). In collect function you add whatever amount you want to the order totals, and fetch() is used for display purposes. If this is done properly, you should see your order total line in the checkout and cart page.

Here we are using two fields fee_amount and base_fee_amount, which contain our fee amount. We will have to see save these two fields to database, so in our module installer file we add this code

Order Page

Till now, all code written has been done only for the quote object. But after order is placed, we need to transfer all information to the order object. As you would have seen above we are using two fields fee_amount and base_fee_amount, we now need to store these two fields in the order table as well. To do all the above we need to do two things. First in the config.xml file add this code inside the global tab,

and in our module install file

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

This is 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.

About Brian Wilson

avatar
Brian is a freelance Magento developer with 5 years of experience with customizing Magento extensions, magento themes. He's interested in sharing Magento Tutorials to the community

12 comments

  1. avatar

    Hi,

    I have installed and configured this module, its working fine withe but I didn’t get fee information on order email and invoice email.

    Please let me know how to fix this?

    Thanks,
    Vimal

  2. avatar

    I just installed this into my magento store and now in my cart page i get this:

    Fatal error: Call to a member function getValue() on a non-object in /home/zippic01/public_html/app/design/frontend/flatshop/edits/template/checkout/cart.phtml on line 111

    Can you help? My store urgently needs to get back working. I tried deleting all the files in the module but it shows the same error

    • avatar

      Hi John,
      Probably the module is oudated, what version of Magento are you using?
      Also, if you delete all files of the modules, your site will work again, please recheck if all files were removed.
      Regards.

      • avatar

        Hi Brian, this module still ok, it just the fee didnt show up on order email or invoice email. Please point us to the right direction to fix this.. thx

  3. avatar

    Hello,

    Thanks for the help.

    I wanted to know can i apply one condition here that if total is less then 499 then add price 50 otherwise do not add.

    So how to get grand total to check.

    Thanks in advance.

  4. avatar

    working fine thanks. but extra fee row is not showing up in order and invoice email plz help.

  5. avatar

    Hi; sorry bad english; I have a problem: My checkout page
    amount =19.90
    fee = 10.00
    delivery price = 5
    but total amount = 36.37

    Why is it incorrect total?

  6. avatar

    I have tried and its showing price into shopping cart but there is error showing before total price.

    Fatal error: Call to a member function setTotal() on a non-object in H:\xampp\htdocs\magento18\app\code\core\Mage\Checkout\Block\Cart\Totals.php on line 63

    Fatal error: Call to a member function setTotal() on a non-object in H:\xampp\htdocs\magento18\app\code\core\Mage\Checkout\Block\Cart\Totals.php on line 74

    How can I solve this issue. Please please help me by giving instruction.

    Thanks

    • avatar

      This error can be corrected by removing
      fee/checkout_total” on line 52 from
      from file Fee/etc/config.xml

      • avatar
        Oluwafemi Korede Lawrence

        Hi, great article you have here, please I’m also having the same issue and I have removed fee/checkout_total from line 52, but it still not working.

        my config.xml directory is Excellence/fee/etc/config.xml

      • avatar

        Hi, that is great module, but same functionality i need into back end when create order from admin panel.
        So please tell me how can set custom extra fee from admin panel when create order.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Scroll To Top