- 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?
Sreenshots:
Download link of the module
https://github.com/yvoronoy/magento-extension-extra-fee
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
1 2 3 4 5 6 7 8 9 10 11 | <global> <sales> <quote> <totals> <fee> <class>fee/sales_quote_address_total_fee</class> </fee> </totals> </quote> </sales> </global> |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | <?php class Magentoexplorer_Fee_Model_Sales_Quote_Address_Total_Fee extends Mage_Sales_Model_Quote_Address_Total_Abstract{ protected $_code = 'fee'; public function collect(Mage_Sales_Model_Quote_Address $address) { parent::collect($address); $this->_setAmount(0); $this->_setBaseAmount(0); $items = $this->_getAddressItems($address); if (!count($items)) { return $this; //this makes only address type shipping to come through } $quote = $address->getQuote(); if(Magentoexplorer_Fee_Model_Fee::canApply($address)){ //your business logic $exist_amount = $quote->getFeeAmount(); $fee = Magentoexplorer_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(array( 'code'=>$this->getCode(), 'title'=>Mage::helper('fee')->__('Fee'), 'value'=> $amt )); return $this; } } |
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
1 2 | ALTER TABLE `".$this->getTable('sales/quote_address')."` ADD `fee_amount` DECIMAL( 10, 2 ) NOT NULL; ALTER TABLE `".$this->getTable('sales/quote_address')."` ADD `base_fee_amount` DECIMAL( 10, 2 ) NOT NULL; |
1 2 3 4 5 6 | <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> |
Next. add these 2 line into module install file
1 2 | ALTER TABLE `".$this->getTable('sales/order')."` ADD `fee_amount` DECIMAL( 10, 2 ) NOT NULL; ALTER TABLE `".$this->getTable('sales/order')."` ADD `base_fee_amount` DECIMAL( 10, 2 ) NOT NULL; |
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 Magento explorer modules file in the link above.
Enjoy coding
1 Comment
If I adding Fee to all products (without conditions). Module disable Free Shipping rule