日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Magento Add Fee or Discount to Order Totals

發(fā)布時(shí)間:2025/3/8 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Magento Add Fee or Discount to Order Totals 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

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.

轉(zhuǎn)載于:https://my.oschina.net/liufeng815/blog/497698

總結(jié)

以上是生活随笔為你收集整理的Magento Add Fee or Discount to Order Totals的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。