Nice to meet you in our Magento blog,

This week I am very happy to hear that one of my followers has passes the exam and got the certificate. It’s really great news that brings me excitement to write the new part in this series. As I mentioned in the previous post, today I’ll continue guide you how to show your custom total in all areas that you see the order. Let’s get started now!

1. Show custom total in frontend order view page

Frontend shows order view in the Customer account page. After customer places orders, he can go to My Orders menu to view his orders. Thus you need to add your total display to this layout page by using the layout handle:

<sales_order_view>
<reference name="order_totals">
<block type="[your_custom_order_total_block]" />
</reference>
</sales_order_view>

And write your total block as follows:

<?php

class [your_custom_order_total_block] extends Mage_Sales_Block_Order_Totals
{
public function initTotals(){
$order = $this->getParentBlock()->getOrder();
if($order->getCustomDiscount() > 0){
$this->getParentBlock()->addTotal(new Varien_Object(array(
'code' => 'custom_discount',
'value' => $order->getCustomDiscount(),
'base_value' => $order->getBaseCustomDiscount(),
'label' => 'Custom Discount',
)),'subtotal');
}
}
}

In this block, you have to write the initTotals method to prepare data for total display.

Also, for invoice view page and credit memo view, you can add your custom total display to the frontend page by using the layout handle sales_order_invoice, sales_order_creditmemo. The blocks for them are similar to the blocks for order.

2. Show custom total in print page

To show custom total in print page, we need to add block to handle layout similarly to displaying it in the view page. The handles you need to add are sales_order_print, sales_order_printinvoice and sales_order_printcreditmemo.

<sales_order_print>
<reference name="order_totals">
<block type="[your_custom_order_total_block]" />
</reference>
</sales_order_print>

Note that you can re-use the total block of the order page view.

3. Show custom total in the email address

Similar to two parts above, to show custom total in the email address, we only need to add block to layout handles (sales_email_order_items, sales_email_order_invoice_items, sales_email_order_creditmemo_items).

 <sales_email_order_items>
<reference name="order_totals">
<block type="[your_custom_order_total_block]" />
</reference>
</sales_email_order_items>

4. Show custom total in backend order view page

To show custom total in backend, you also have to add block to layout handle. However, the handle is in the admin layout and the block is as same as the block in frontend.

<adminhtml_sales_order_view>
<reference name="order_totals">
<block type="[your_custom_order_total_block]" />
</reference>
</adminhtml_sales_order_view>

For the new invoice page, invoice view page, new creditmemo page, creditmemo page, you can use handles: adminhtml_sales_order_invoice_new, adminhtml_sales_order_invoice_view, adminhtml_sales_order_creditmemo_new, adminhtml_sales_order_creditmemo_view.

5. Show custom total in PDF (printed from backend)

The way to show your custom total in PDF is different from all parts above. You need to use the configuration (config.xml) to add the custom total pdf model:

<global>
<pdf>
<totals>
<custom_discount translate="title">
<title>Custom Total</title>
<source_field>custom_discount</source_field>
<model>[custom_total_pdf_model]</model>
<font_size>7</font_size>
<display_zero>0</display_zero>
<sort_order>251</sort_order>
</custom_discount>
</totals>
</pdf>
</global>

And write the PDF total model as below:

<?php
class [custom_total_pdf_model] extends Mage_Sales_Model_Order_Pdf_Total_Default
{
public function getTotalsForDisplay(){
$amount = $this->getOrder()->formatPriceTxt($amount);
if ($this->getAmountPrefix()){
$discount = $this->getAmountPrefix().$discount;
}
$totals = array(array(
'label' => 'Custom Discount'
'amount' => $amount,
'font_size' => $fontSize,
)
);
return $totals;
}
}
}

My tutorial today seems too long and I am about to end it here. Hope that you can show your custom total in Magento by yourself after reading. If having any difficulties or problems, just let me know. If you are interested in Magento certification exam, just go to our Magento blog.

Nice code!

Author

Why Magestore? We believe in building a meaningful & long-term relationship with you.

21 Comments

  1. Hi! I will be taking the exam next week. I have read all your articles and I hope this will be useful. Any other pointers you could me? Do I need to read the core classes? It’s really huge. thank you and more power!

    • Hi Blake,

      Thanks much for following my posts. I think if you have time, you should refer to the code in Magento core for better preparation. Good luck and hope to hear good news from you soon!

  2. Hello David,

    I’m here again with another question, as I’ve terminated the previous tutorial and now I’m trying to show my custom discount also in my users orders detail.

    I’ve created my class Company_Module_Block_Order_Totals (extending Mage_Sales_Block_Order_Totals) under app/code/local/Company/Module/Block/Order/Totals.php. For now in its initTotals() method there is only a var_dump(“string”); to test if it does something.
    I have then added the following lines in my app/code/local/Company/Module/etc/config.xml

    […]

    Company_Module_Block

    […]

    Then I’ve created the config file app/design/frontend/default/customtheme/layout/sales.xml and have used your example:

    Flushing the cache and going to does not show the orders list table (this could mean that something has been overwritten, I suppose, but not completely). The same if I try to call the url of an order detail (it shows an empty page).

    What am I missing? Or what have I done wrong?

    Thanks again for your patience and help!

  3. Sorry, but it didn’t keep the xml file code… How can I post it to you?

    • Hi Alessandro,
      You can use var_dump() in your total block. After using this command, you need to add die() command to view your dump data. If you don’t keep the xml file code, there’s no way that I can check it for you. However, feel free to let me know if you have any other issue. I’ll glad to help you.

  4. Hello, I’ve put exit() instead of die(), I suppose it’s the same. How can I post here my xml code? Tags are stripped… Can I send you by email? How?
    Thanks

  5. Hi David,

    I’ve sent you an e-mail to the address specified! Let me know if you understand it 🙂

    Thanks

    • Hi Alessandro,
      I’ve received your email and view your code. Instead of writing code in file app/design/frontend/default/customtheme/layout/sales.xml, you have to put it in the file: app/design/frontend/default/default/layout/module.xml
      Also, your code lacks of the tag
      Let me know if you have any question. Hope you succeed!

  6. Thanks David. The only thing I have not understood is the tag that my code lacks, it’s been stripped from your message 🙂

    Regarding the path app/design/frontend/default/default/layout/module.xml, module.xml should be the real filename or should I use the name of my module? Have I to use the “default” template or could I use also a custom template?

    Sorry for all these questions, but I feel a bit confused.

    Thanks again.

    • Hi Alessandro,
      I’m so sorry. It’s my mistake. The tag that your code lacks is <reference name=”order_totals”>. Also, you can use the Netbean IDE to check xml syntax.
      Regarding the path app/design/frontend/default/default/layout/module.xml, module.xml, the module has the lowercase module name (the layout file of your module). You should write in “default” template. It works for almost cases. Hope it helps.

  7. Hello David, you are right, the tag was missing in my email but not in my code, so I really don’t know what’s my mistake, I’ve tried hundreds of things and nothing seems to make it work… I think I’ll give up. Thanks anyway for your support, you’ve been very kind to me.

  8. Hello, I’ve solved it! I think it was a problem in the type attribute of the block tag in the xml file: instead of type=”company_module_order_totals” should be type=”company_module/order_totals”. I also specified the config filename in the config.xml in the frontend->layout->updates section.

    Now it can see the custom block class, but for I don’t know which reason, it does not get the $order ($this->getParentBlock()->getOrder()): if I put the variable into a var_dump() nothing is returned…

  9. Hi, I’ve discovered that using $this->getOrder() instead of $this->getParentBlock()->getOrder() works! Thanks for all your support!

  10. Hi David! Magento doesn’t insert custom total in the email templates. Can you suggest something?

    • Hello Anna,
      Regarding your question, you can add the block total to the following handles:
      sales_email_order_items
      sales_email_order_invoice_items
      sales_email_order_creditmemo_items
      We also mentioned this in section 3.3. Show custom total in the email address of this post.
      Hope you succeed!

    • Hello Anna,
      Actually, I can not define the reason for your issue without reviewing your code.

  11. Great post and very well explanation.
    Its clear to understand and very help full to all magento developer.
    Thanks for sharing such a great knowledge with us.

  12. Vuong Hoang Reply

    Hi David, in section “Show custom total in PDF (printed from backend)”, i think should override method getAmount() of class Mage_Sales_Model_Order_Pdf_Total_Default, because it is simple than overriding method getTotalsForDisplay().

    • Hi Vuong Hoang,

      Thank you very much for your comment.
      Actually, we wrote the wrong code. Now, I have updated it.
      Please take a look.
      By the way, we are going to continue our Tutorial posts after a long time of interaction.
      So it would be great if we could have your concern.

      Have fun ^^

Write A Comment