Magento Add Fee or Discount to Order Totals

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

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 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

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

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

<?php
class Excellence_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(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(array(
				‘code‘=>$this->getCode(),
				‘title‘=>Mage::helper(‘fee‘)->__(‘Fee‘),
				‘value‘=> $amt
		));
		return $this;
	}
}

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

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;

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,

<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( 10, 2 ) NOT NULL;
	ALTER TABLE  `".$this->getTable(‘sales/order‘)."` ADD  `base_fee_amount` DECIMAL( 10, 2 ) NOT NULL;

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.

时间: 2024-10-18 20:34:32

Magento Add Fee or Discount to Order Totals的相关文章

EBS APIS(转)

API To Find Sales Order's Subtotal,discount,charges and Tax (OE_OE_TOTALS_SUMMARY.ORDER_TOTALS) API for Cancelling the Purchase Order (PO) Document (PO_DOCUMENT_CONTROL_PUB.CONTROL_DOCUMENT) API for Deleting the category assignment to an item (INV_IT

mycat 分页慢原理解析、mycat跨事务解惑、mycat注解调用存储过程分析

1结合Mycat日志,分析select * from travelrecord order by id limit100000,100 的运行过程,解释下当limit M,N中的M非常大的情况下.为什么查询结果会变慢非常多 1.1mycat控制命令台显示.explain出来走了全部的节点 mysql>explain select * from travelrecord order by id limit 100000,100 ; +-----------+--------------------

[转]Oracle EBS APIs

FROM:http://blog.csdn.net/pan_tian/article/details/7754598 API To Find Sales Order's Subtotal,discount,charges and Tax (OE_OE_TOTALS_SUMMARY.ORDER_TOTALS) API for Cancelling the Purchase Order (PO) Document (PO_DOCUMENT_CONTROL_PUB.CONTROL_DOCUMENT)

Oracle EBS APIs

http://blog.csdn.net/pan_tian/article/details/7754598 API To Find Sales Order's Subtotal,discount,charges and Tax (OE_OE_TOTALS_SUMMARY.ORDER_TOTALS) API for Cancelling the Purchase Order (PO) Document (PO_DOCUMENT_CONTROL_PUB.CONTROL_DOCUMENT) API f

Drools文档(八) 规则语言参考

规则语言参考 概述 Drools有一个"本地"的规则语言.这种格式在标点符号上非常轻,并且通过"扩展器"支持自然语言和领域特定的语言,使语言能够变形到您的问题领域.本章主要与本机规则格式一致.用于表示语法的图表被称为"铁路"图表,它们基本上是语言术语的流程图.技术上非常热衷的也可以参考DRL.g这是规则语言的Antlr3语法.如果您使用Rule Workbench,则可以通过内容帮助为您完成许多规则结构,例如,输入"ru"并按

笔记:Hibernate DML

Hibernate 提供的HQL(Hibernate Query Language)语句也支持批量 update 和 delete 语法,语法格式如下: [UPDATE | DELETE] FROM <ClassName> [WHERE conditions] 关于上面的语法格式有以下需要值得注意的: 在 FROM子句中,FROM 关键字是可选的,可以不写 FROM 关键字 在 FROM 子句中,只能有一个类名,可以在该类名后指定别名 不能在批量HQL语句中使用连接,显式或者隐式的都不行,但可

[Javascript] bukld &#39;SQL&#39; like object tree

Let's try creating a deeper tree structure. This time we have 4 separate arrays each containing lists, videos, boxarts, and bookmarks respectively. Each object has a parent id, indicating its parent. We want to build an array of list objects, each wi

【转】Predicate和Consumer接口– Java 8中java.util.function包下的接口

原文链接 http://ifeve.com/predicate-and-consumer-interface-in-java-util-function-package-in-java-8/ 原文链接 作者:   Mohamed Sanaulla  译者: 李璟([email protected]) 早先我写了一篇<函数式接口>,探讨了部分Java 8中函数式接口的用法.我也提及了Predicate接口属于java.util.function包, 在这篇文章中,我将展示如何应用Predicat

hibernate 常用几种查询

在传统行业中,hibernate作为常用的传统ORM框架还是有很多的开发者在使用.个人觉得下面几点原因, 应对传统的关系型数据库,hibernate在处理数据之间的check约束上还是有他的优势.通过使用传统的el表达式在展现页面的时候发现还是比较方便的. hibernate 屏蔽了开发者在Dao层面的大部分问题,能够使开发者更加的去专注于业务的开发和逻辑的实现.这个过程包括自动的物理表之间的关系约束建立,实体的自动映射. hibernate 能够配置固定的数据库连接,使得程序在使用过程中提高一