2019独角兽企业重金招聘Python工程师标准>>>

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.

转载于:https://my.oschina.net/liufeng815/blog/497698

Magento Add Fee or Discount to Order Totals相关推荐

  1. magento 删除所有用户订单 delete order customer

    2019独角兽企业重金招聘Python工程师标准>>> SET FOREIGN_KEY_CHECKS=0;-- Here's where we reset the orders TR ...

  2. magento2 checkout totals添加产品属性

    totals items的数据流程 MagentoQuoteModelCartTotalsItem(MagentoQuoteApiDataTotalsItemInterface) MagentoChe ...

  3. Magento 手机支付 (支付宝无线支付)

    现在magento 支付宝插件在网上还是很多的.但是支付宝无线支付貌似还不多.这里我只是根据官方文档和其它 支付方式编写出无线支付的雏形,可用但是不完善,先记录在这里.(当然文章是点到为止,只给我这种 ...

  4. magento:DirectoryIndex not allowed here

    在window xp上使用magento.环境按magento需求的环境进行配置. 然后访问http://localhost/magento页面时出现以下错误: Internal Server Err ...

  5. SAP Spartacus i18n 的文本,和翻译相关的话题:internationalization

    存储在这个config对象里: 英文版的资源源代码如下: {"fallbackLang":"en","debug":false," ...

  6. eShopOnContainers 知多少[11]:服务间通信之gRPC

    1. 引言 最近翻看最新3.0 eShopOncontainers源码,发现其在架构选型中补充了 gRPC 进行服务间通信.那就索性也写一篇,作为系列的补充. 2. gRPC 老规矩,先来理一下gRP ...

  7. tsql语句中的t是什么_TSQL中的Java HashCode

    tsql语句中的t是什么 The Java HashCode method is used to determine uniqueness or similarity of strings. Whil ...

  8. 电报注册网络代理_如何在电报开放网络(TON)中开发和发布智能合约

    电报注册网络代理 这篇文章是关于什么的? (What is this article about?) In this article, I will tell about my participati ...

  9. Drools Accumulate 语法解析及使用

    一.Accumulate语法分析 accumulate( <source pattern>; <functions> [;<constraints>] ) 例如,计 ...

最新文章

  1. 06/05/2015
  2. DL之VGG16:基于VGG16(Keras)利用Knifey-Spoony数据集对网络架构FineTuning
  3. ERROR: 资源短缺 PXA_NO_FREE_SPACE
  4. SpringBoot+ElementUI实现通用文件下载请求(全流程图文详细教程)
  5. Could not create JarEntryRevision
  6. 【转载保存】Java丨jsoup网络爬虫登录得到cookie并带上cookie访问
  7. 十三、JSP9大隐视对象中四个作用域的大小与作用范围
  8. python算法应用(二)——一些使用技巧
  9. UML建模【转http://www.cnblogs.com/gaojun/archive/2010/04/27/1721802.html】
  10. webbench接口并发测试
  11. XTU OJ 1352 Fraction
  12. C++ 产生0-1之间的随机数
  13. 小程序中自定义图片预览功能
  14. 英飞凌基础学习笔记(GTM)Generic Timer Module
  15. Hashcat使用指南
  16. java 数组 协变类型6_Java漫谈-协变返回类型
  17. PHP是核心思路,模拟OICQ的实现思路和核心程序(一)_php基础
  18. 计算机更换桌面图标,Win10系统如何更换桌面图标呢?电脑更换桌面程序图标的操作方法...
  19. Nginx 指定多个域名跨域请求配置
  20. JESD79-4 第4章 SDRAM命令描述与操作(4.5-4.7)

热门文章

  1. python20个常用语法_这20个常规Python语法你都搞明白了吗?
  2. c语言api_用C语言来拓展python的功能
  3. php浏览服务器某一文件夹内容,php删除web服务器中指定目录下的指定格式的文件...
  4. 安装华为鸿蒙,魅族选择与鸿蒙合作,华为这下要成了?
  5. Eclipse 答疑:Eclipse 使用 Amateras UML 创建类图点击 Finish 没反应解决方式汇总
  6. 《软件项目管理(第二版)》第 8 章——项目团队与干系人 重点部分总结
  7. 《软件需求分析(第二版)》第 16 章——需求链中的联系链 重点部分总结
  8. C语言/C++编程学习:不找C/C++的工作也要学C/C++的原因
  9. linux查看内存_嵌入式操作系统的内存,你了解多少?
  10. php实现分页mssql,PHP操作mssql数据库类,含分页类[分页类原创]