0

So far I could add a custom fee to an order. It shows on checkout page, admin order view, admin invoice view, invoice pdf. But I don't know how I should do to display it in order/invoice email.

enter image description here

I see one way to do this is: override _initTotals method of Mage_Sales_Block_Order_Totals class. But I don't know if this will affect another places, or if other extension wants to do the same thing (adding some custom fees to the totals block), then they will conflict.

I doubted that there's a way to add a total to totals block via xml, or binding to some event...

Given the fact that there's a column called customfee_total in sales_flat_order table, what's the best way to achieve that?

2

I found the answer.

Add the following code to your module config.xml

<config>
...
   <frontend>
        <layout>
            <updates>
                <customfee>
                    <file>customfee.xml</file>
                </customfee>
            </updates>
        </layout>
    </frontend>
...
</config>

add app/design/frontend/base/default/layout/customfee.xml

<?xml version="1.0"?>
<layout>
    <sales_email_order_items>
        <reference name="order_totals">
            <block type="customfee/sales_totals_order_customfee" name="customfee"/>
        </reference>
    </sales_email_order_items>

    <sales_email_order_invoice_items>
        <reference name="invoice_totals">
            <block type="customfee/sales_totals_order_invoice_customfee" name="customfee"/>
        </reference>
    </sales_email_order_invoice_items>

    <sales_email_order_creditmemo_items>
        <reference name="creditmemo_totals">
            <block type="customfee/sales_totals_order_creditmemo_customfee" name="customfee"/>
        </reference>
    </sales_email_order_creditmemo_items>
</layout>

Example app/code/local/Cream/CustomFee/Block/Sales/Totals/Order/Customfee.php

class Cream_CustomFee_Block_Sales_Totals_Order_Customfee extends Mage_Core_Block_Template {
  public function initTotals() {

    if((float) $this->getParentBlock()->getSource()->getCustomfeeTotal() == 0) {
      return $this;
    }

    $total = new Varien_Object(array(
      'code'  => $this->getNameInLayout(),
      'value' => $this->getParentBlock()->getSource()->getCustomfeeTotal(),
      'label' => Mage::helper('customfee')->getName()
    ));

    $after = 'subtotal';

    $this->getParentBlock()->addTotal($total, $after);
    return $this;
  }
}
| improve this answer | |
  • How solve it for Magento 2 ? – Jsparo30 Feb 13 '18 at 12:40
1

follow this tutorial

you have to override sales_order_invoice_totals

<global>
     <sales>

         <order_invoice>
             <totals>
                 <turnkeye_insurance>
                     <class>turnkeye_insurance/total_screen_invoice</class>
                     <after>subtotal</after>
                     <before>tax</before>
                 </turnkeye_insurance>
             </totals>
         </order_invoice>

     </sales>
 </global> 




class Turnkeye_Insurance_Model_Total_Screen_Invoice extends Mage_Sales_Model_Order_Invoice_Total_Abstract
{
    public function collect(Mage_Sales_Model_Order_Invoice $invoice)
    {
        $order = $invoice->getOrder();
        $amount = INSURANCE_FEE; //add your fee here
        if ($amount) {
            $invoice->setGrandTotal($invoice->getGrandTotal() + $amount);
            $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $amount);
        }

        return $this;
    }
}
| improve this answer | |
  • 2
    sorry but this is not the right answer, this only adds a row to invoice totals (admin order view), not the email – Thịnh Phạm Jan 15 '16 at 8:24
  • @ThịnhPhạm this is also add row in invoice total. – Qaisar Satti Jan 15 '16 at 9:33
  • @QaisarSatti could you pls help me @ magento.stackexchange.com/q/212197/57334 – zus Feb 6 '18 at 4:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.