Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Read below I think this very help full to you:-</strong></p> <p><strong>See url Below</strong></p> <p><a href="http://www.excellencemagentoblog.com/magento-add-fee-discount-order-total" rel="nofollow">http://www.excellencemagentoblog.com/magento-add-fee-discount-order-total</a></p> <p><strong>Magento Add fee or discount order total</strong></p> <p>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</p> <ul> <li><strong>Checkout Page Order Total</strong></li> <li><strong>Cart Page Order Total</strong></li> <li><strong>My Account Order View Page</strong></li> <li><strong>Order EMails</strong></li> <li><strong>Admin Order View/Email/PDF</strong></li> <li><strong>Admin Invoice View/Email/PDF</strong></li> <li><strong>Admin Credit Memo View/Email/PDF</strong></li> </ul> <p>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> <p><strong>Or try it:-</strong></p> <p><strong>Checkout Page Total Order Total Basics</strong></p> <p>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</p> <pre><code>&lt;global&gt; &lt;sales&gt; &lt;quote&gt; &lt;totals&gt; &lt;fee&gt; &lt;class&gt;fee/sales_quote_address_total_fee&lt;/class&gt; &lt;/fee&gt; &lt;/totals&gt; &lt;/quote&gt; &lt;/sales&gt; &lt;/global&gt; </code></pre> <p>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</p> <pre><code>&lt;?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-&gt;_setAmount(0); $this-&gt;_setBaseAmount(0); $items = $this-&gt;_getAddressItems($address); if (!count($items)) { return $this; //this makes only address type shipping to come through } $quote = $address-&gt;getQuote(); if(Excellence_Fee_Model_Fee::canApply($address)){ //your business logic $exist_amount = $quote-&gt;getFeeAmount(); $fee = Excellence_Fee_Model_Fee::getFee(); $balance = $fee - $exist_amount; $address-&gt;setFeeAmount($balance); $address-&gt;setBaseFeeAmount($balance); $quote-&gt;setFeeAmount($balance); $address-&gt;setGrandTotal($address-&gt;getGrandTotal() + $address-&gt;getFeeAmount()); $address-&gt;setBaseGrandTotal($address-&gt;getBaseGrandTotal() + $address-&gt;getBaseFeeAmount()); } } public function fetch(Mage_Sales_Model_Quote_Address $address) { $amt = $address-&gt;getFeeAmount(); $address-&gt;addTotal(array( 'code'=&gt;$this-&gt;getCode(), 'title'=&gt;Mage::helper('fee')-&gt;__('Fee'), 'value'=&gt; $amt )); return $this; } } </code></pre> <p>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</p> <pre><code>ALTER TABLE `".$this-&gt;getTable('sales/quote_address')."` ADD `fee_amount` DECIMAL( 10, 2 ) NOT NULL; ALTER TABLE `".$this-&gt;getTable('sales/quote_address')."` ADD `base_fee_amount` DECIMAL( 10, 2 ) NOT NULL; </code></pre> <p><strong>Order Page</strong></p> <p>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,</p> <pre><code>&lt;fieldsets&gt; &lt;sales_convert_quote_address&gt; &lt;fee_amount&gt;&lt;to_order&gt;*&lt;/to_order&gt;&lt;/fee_amount&gt; &lt;base_fee_amount&gt;&lt;to_order&gt;*&lt;/to_order&gt;&lt;/base_fee_amount&gt; &lt;/sales_convert_quote_address&gt; &lt;/fieldsets&gt; </code></pre> <p>and in our module install file</p> <pre><code>ALTER TABLE `".$this-&gt;getTable('sales/order')."` ADD `fee_amount` DECIMAL( 10, 2 ) NOT NULL; ALTER TABLE `".$this-&gt;getTable('sales/order')."` ADD `base_fee_amount` DECIMAL( 10, 2 ) NOT NULL; </code></pre> <p>After doing this, these two fields should get saved to the order table from the quote table.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload