Wednesday, May 14, 2014

Include the text "Grand Total to be Charged" in Invoice Emails in Magento 1.7

I had this requirement in Magento 1.7 a couple of days back. Suppose, my site has multiple currencies and I check out with GBP whereas my default currency is USD. Now, the Order email has the following texts ::

...                          ... 
Grand Total                  £93.66
Grand Total to be Charged    $169.70

Which means, I purchased items costing £93.66 but I will be charged $169.70 (which is equal to £93.66 as per conversion rate on that day). But when the Invoice mail is generated, the last line "Grand Total to be Charged" is not appearing. I needed that line to appear on all other transaction emails including Invoice emails.

How I found the solution ?

To solve the problem, I started digging into the email template files first. Order_new.html and invoice_new.html were the files I started to delve in. My observations are as shown below :

1. The Invoice email template invoice_new.html has the following line ( which deals with and prints the whole PRICE stuffs ):
     
    {{layout handle="sales_email_order_invoice_items" invoice=$invoice order=$order}}

   whereas the Order email template order_new.html has the following line :

   {{layout handle="sales_email_order_items" order=$order}}

2. In sales.xml, layout handle 'sales_email_order_invoice_items' has a block template 'email/order/invoice/items.phtml'. This item.phtml file has the following line ::

    echo $this->getChildHtml('invoice_totals');

and sales.xml again says ( getChildHtml('invoice_totals') is referring to )::
   
    <block type="sales/order_invoice_totals" name="invoice_totals" template="sales/order/totals.phtml">   

3. Again, in sales.xml, layout handle 'sales_email_order_items' has a block template 'email/order/items.phtml' .. this item.phtml file has the following line ::

   echo $this->getChildHtml('order_totals');

and sales.xml again says getChildHtml('order_totals') is referring to ) :: 
   
<block type="sales/order_totals" name="order_totals" template="sales/order/totals.phtml">

4. So, both Order and Invoice emails dynamically loads "sales/order/totals.phtml" file. Now in totals.phtml file, we have a line like this :: 

foreach ($this->getTotals() as $_code => $_total): 

The above foreach construct takes output (as an array) from getTotals() function and loops through the output array containing whole Pricing structure including base price, shipping expenses, tax, discounts, Base price in other currencies etc. 

5. As the Base Price (with BASE currency as set in Admin Panel ) was coming properly in Order email, I checked the class ( from <block type='sales/order_totals' ) Mage_Sales_Block_Order_Totals found in Mage/Sales/Block/Order/Totals.php. The getTotals() function is just iterating the class property '$this->_totals'. I also checked how this '$this->_totals' array was created through '_initTotals()' method. And within this '_initTotals()' method, $this->_totals['base_grandtotal'] is being created which is responsible for printing the Order price in Base Currency and this is what I was looking for. However, this Base price was not coming in Invoice emails. Hence I opened the Class 'Mage_Sales_Block_Order_Invoice_Totals'.

6. The class 'Mage_Sales_Block_Order_Invoice_Totals' ( existing in file 'app/code/core/Mage/Sales/Block/Order/Invoice/Totals.php' ) inherits from the class 'Mage_Sales_Block_Order_Totals' and overrides the function _initTotals() which created the pricing array _totals. Check the function below :

    protected function _initTotals()
  {
     // Call Parent Mage_Sales_Block_Order_Totals
     // class' _initTotals() to create the 
     // parent class' _totals array
     parent::_initTotals();
        
     // Removing the _totals['base_grandtotal']
     $this->removeTotal('base_grandtotal');
     return $this;
  }
    
    The line $this->removeTotal('base_grandtotal'); is removing the Base Price section from the _totals array. Simply commenting the line finally solved my problem.

1 comment:

Jeeva Rathinam said...

How can i remove that line?