How to Add fee or discount to order totals in Magento 1.9

Sometimes you might want to add some additional fee or discount to order total in Magento. In this tutorial, I will guide you how to add an additional line to your Magento order in checkout step. Normally, an order in Magento includes Subtotal, Discount, Shipping cost and Taxes, these values will make the total order.
Now we will add a new kind fee or discount, this fee/discount will be included in many pages/views:
  • Checkout Page
  • Cart Page
  • My Account Order View Page
  • Admin Order View
  • Admin Invoice View
  • ………

In this tutorial for developer, I will just include a very basic extension to add an extra change and you can base on my simple extension to add more advanced kind of fee or discount to your Magento store order total. I will explain step by step how to do this so that you can completely understand this module.

Quick FAQs

What will this module do?

This module will add an extra fee to your order total with a fixed value, in this tutorial I will set it: $5

Note: Since this is a very basic module, you will see the extra fee only when placing order in front end.

Work with Magento version?

Tested with Magento 1.6, 1.7, 1.8, 1.9

What does the module look like?


add fee discount magento 1

add fee discount magento 1 view page

Download link of the module

Now let’s start

Checkout Page Total Order Total Basics

Checkout page is the most important page so, frist, we will start to learn how to add fee to only checkout page. The lines in order totals of checkout page are included in the files located in folder Mage\Sales\Model\Quote\Address\Total.

Before an order is made, data of oder is kept in a quote object, order data will be moved to order object after the order is placed. Collector patterns set the quote totals and and many collector classes can be added. To add collector to the quote object, we add the following code in config.xml

When the totals are are determined for a quote

This means whenever the totals are determined for a quote, it will also call the class we   fee/sales_quote_address_total_fee .

Next, add the following code to our collector class

collect()  and fetch()  are the 2 main functions here. With  collect() function, you can add the value you want to order totals, while fetch() function will display the value. When every is finished, you will see a the order totals line in checkout and cart page.

We use fee_amount and base_fee_amount as it contains the fee amount we want to add. Now, to save there value to database, in module installer file add these lines

Order Page
All the above steps are for quote object online. Now we need to move all information to the order object. What we need to do now is to store 2 field fee_amount and base_fee_amount into order table
First, add this code to global tab in config.xml

Next. add these 2 line into module install file

Now the 2 field fee_amount and base_fee_amount were saved to quote table

Some last words

This tutorial help you understand the basis of adding an extra fee/discount to Magento. In order to add more advanced fields, please go through the modules file in the link above.

Enjoy coding


About Author

Brian is a Magento developer with 5 years of experience with customizing Magento extensions, Magento templates. He's main author at Magento explorer

Leave A Reply