Magento Add Fee or Discount to Order Totals

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

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/544287.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

再见 Feign!推荐一款微服务间调用神器,跟 SpringCloud 绝配!

在微服务项目中&#xff0c;如果我们想实现服务间调用&#xff0c;一般会选择Feign。之前介绍过一款HTTP客户端工具Retrofit&#xff0c;配合SpringBoot非常好用&#xff01;其实Retrofit不仅支持普通的HTTP调用&#xff0c;还能支持微服务间的调用&#xff0c;负载均衡和熔断限…

Spring Cloud Alibaba Nacos 的 2 种健康检查机制!

作者 | 磊哥来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;Spring Cloud Alibaba Nacos 作为注册中心不止提供了服务注册和服务发现功能&#xff0c;它还提供了服务可用性监测的机制。…

Python之包管理工具

在Python环境中已经有很多成熟的包&#xff0c;可以通过安装这些包来扩展我们的程序。 例如&#xff0c;很多时候Python开发人员都会去PyPI网站去查找自己想要使用的包&#xff0c;然后进行安装。PyPI &#xff08; Python Package Index&#xff09;是获得第三方 Python 软件包…

为什么wait和notify必须放在synchronized中?

作者 | 磊哥来源 | Java面试真题解析&#xff08;ID&#xff1a;aimianshi666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;在多线程编程中&#xff0c;wait 方法是让当前线程进入休眠状态&#xff0c;直到另一个线程调用了 notify 或 notify…

聊聊并发编程的10个坑

前言对于从事后端开发的同学来说&#xff0c;并发编程肯定再熟悉不过了。说实话&#xff0c;在java中并发编程是一大难点&#xff0c;至少我是这么认为的。不光理解起来比较费劲&#xff0c;使用起来更容易踩坑。不信&#xff0c;让继续往下面看。今天重点跟大家一起聊聊并发编…

macbook终端使用记(二)终端快捷键

为什么80%的码农都做不了架构师&#xff1f;>>> Command K清屏 Command T新建标签 Command M最小化窗口 Command W 关闭当前标签页 Command S 保存终端输出 Command D 垂直分隔当前标签页 Command Shift D 水平分隔当前标签页 Command shift {或}向左/向…

颜值爆表!Redis 官方可视化工具来啦,功能真心强大!

最近逛了一下Redis官方网站&#xff0c;发现Redis不仅推出了很多新特性&#xff0c;而且还发布了一款可视化工具RedisInsight。试用了一下感觉非常不错&#xff0c;最关键的是能支持RedisJSON之类的新特性&#xff0c;这是第三方工具无法比拟的。今天带大家体验一下RedisInsigh…

20个响应式网页设计中的“神话”误区

关于响应式网页的重要性我们已经证实了很长时间了&#xff0c;现在是该把焦点放到如何做出好的响应式网页设计的时候了。一起来看看吧&#xff01; 虽然很多人都在谈论响应式网页&#xff0c;但并不是每个人都知道他们在说什么。很多时候你看到网上的一些信息也在挑战你对响应式…

MySQL 索引失效的 15 种场景!

背景 无论你是技术大佬&#xff0c;还是刚入行的小白&#xff0c;时不时都会踩到Mysql数据库不走索引的坑。常见的现象就是&#xff1a;明明在字段上添加了索引&#xff0c;但却并未生效。前些天就遇到一个稍微特殊的场景&#xff0c;同一条SQL语句&#xff0c;在某些参数下生效…

Java夺命21连问!(附答案)

大家好&#xff0c;我是磊哥。有位朋友工作三年&#xff0c;去面试&#xff0c;给大家整理一下面试题&#xff0c;并附上答案。Mysql索引在什么情况下会失效MySql的存储引擎InnoDB与MyISAM的区别Mysql在项目中的优化场景&#xff0c;慢查询解决等Mysql有什么索引&#xff0c;索…

SpringCloud Nacos + Ribbon 调用服务的 2 种方法!

作者 | 磊哥来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;在 Nacos 中&#xff0c;服务调用主要是通过 RestTemplate Ribbon 实现的&#xff0c;RestTemplate 是 Spring 提供的 Rest…

SpringCloud Ribbon中的7种负载均衡策略!

作者 | 磊哥来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;负载均衡通器常有两种实现手段&#xff0c;一种是服务端负载均衡器&#xff0c;另一种是客户端负载均衡器&#xff0c;而我们…

线程池是如何执行的?拒绝策略有哪些?

作者 | 磊哥来源 | Java面试真题解析&#xff08;ID&#xff1a;aimianshi666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;聊到线程池就一定会聊到线程池的执行流程&#xff0c;也就是当有一个任务进入线程池之后&#xff0c;线程池是如何执…

浮动元素的均匀分布和两端对齐

当我们使用float来使元素并排显示的时候&#xff0c;可以使用margin来控制元素之间的距离&#xff0c;而在很多版式里&#xff08;例如产品图片的列表&#xff09;&#xff0c;需要浮动的元素达到两端对齐的效果&#xff0c;如图1所示。 图1 两端对齐的版式 单纯使用float:left…

20 图|Nacos 手摸手教程

Nacos 作为服务注册中心、配置中心&#xff0c;已经非常成熟了&#xff0c;业界的标杆&#xff0c;在讲解 Nacos 的架构原理之前&#xff0c;我先给大家来一篇开胃菜&#xff1a;讲解 Nacos 如何使用。涉及到如下两个话题&#xff1a;用 Nacos 作为注册中心。用 Nacos 作为配置…

为什么Spring需要三级缓存解决循环依赖,而不是二级缓存?

来源&#xff1a;https://www.cnblogs.com/semi-sub/p/13548479.html在使用spring框架的日常开发中&#xff0c;bean之间的循环依赖太频繁了&#xff0c;spring已经帮我们去解决循环依赖问题&#xff0c;对我们开发者来说是无感知的&#xff0c;下面具体分析一下spring是如何解…

20款华丽的几何形状字体【免费下载】

这里手机的字体使用几何形状设计。流畅简洁的线条&#xff0c;完美的圆形的角度建立一个完整性的设计感。使用几何形状生成出每一个优雅而现代的字母。这些字体可以用于标题和正文。由于他们的设计适合任何干净简约设计&#xff0c;因此很受欢迎。向下滚动并下载这些免费几何字…

MySQL 精选 60 道面试题(含答案)

金三银四到了&#xff0c;给大家整理一些数据库必知必会的面试题。基础相关1、关系型和非关系型数据库的区别&#xff1f;关系型数据库的优点容易理解&#xff0c;因为它采用了关系模型来组织数据。可以保持数据的一致性。数据更新的开销比较小。支持复杂查询&#xff08;带 wh…

Spring Boot 如何解决多个定时任务阻塞问题?

大家好&#xff0c;我是不才磊哥~最近长文撸多了&#xff0c;有点累&#xff0c;今天来点简单的。今天这篇文章介绍一下Spring Boot 中 如何开启多线程定时任务&#xff1f;为什么Spring Boot 定时任务是单线程的&#xff1f;想要解释为什么&#xff0c;一定要从源码入手&#…

mysql之explain

⊙ 使用EXPLAIN语法检查查询执行计划 ◎ 查看索引的使用情况 ◎ 查看行扫描情况⊙ 避免使用SELECT * ◎ 这会导致表的全扫描 ◎ 网络带宽会被浪费话说工欲善其事&#xff0c;必先利其器。今天就简单介绍下EXPLAIN。 内容导航 idselect_typetabletypepossible_keyskeyke…