2019独角兽企业重金招聘Python工程师标准>>>
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.
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;
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.