How to Add New Total to Email in Magento 2

Hello guys! The tutorial today will help the Magento 2 merchants to add the new total block into order email by themselves. As it is necessary when sending your customers the verified email to confirm their purchasing on your store. And your company will be even more reliable when informing your shoppers with the total purchase orders. However, Magento 2 does not support the available feature of adding the new total block into the confirmation email or email of invoice. Hence, let’s follow the guidance in this post to setup the total block into the order email before sending it to your customers.

Add New Total to Email

  • Step 1: Generate the file: sales_email_order_items.xml
  • Step 2: Create the block new.order.totals

Step 1: Generate the file: sales_email_order_items.xml

  • Create the file: sales_email_order_items.xml in the etc/frontend folder:
<?xml version="1.0" encoding="UTF-8"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
   <body>
       <referenceBlock name="order_totals">
           <block class="Mageplaza\HelloWorld\Block\Order\Totals" name="new.order.totals" />
       </referenceBlock>
   </body>
</page>

<?xml version="1.0" encoding="UTF-8"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
   <body>
       <referenceBlock name="order_totals">
           <block class="Mageplaza\HelloWorld\Block\Order\Totals" name="new.order.totals" />
       </referenceBlock>
   </body>
</page>
  • Now the block new.order.totals have been inserted to the order_totals when sending the email.

Step 2: Create the block new.order.totals

  • Create the block new.order.totals to show the information for the new total.
  • Create the file : Mageplaza\HelloWorld\Block\Order\Totals.php
  • In the initTotals() function, insert your logic to calculate the amount which you want to show in the email.
<?php
namespace Mageplaza\HelloWorld\Block\Order;
class Totals extends \Magento\Framework\View\Element\AbstractBlock
{
   public function initTotals()
   {
       $orderTotalsBlock = $this->getParentBlock();
       $order = $orderTotalsBlock->getOrder();
       if ($order->getNewTotalAmount() > 0) {
           $orderTotalsBlock->addTotal(new \Magento\Framework\DataObject([
               'code'       => 'new_total',
               'label'      => __('New Total'),
               'value'      => $order->getNewTotalAmount(),
               'base_value' => $order->getNewTotalBaseAmount(),
           ]), 'subtotal');
       }
   }
}
  • Save the file.


Module Development Series



Enjoyed the tutorial? Spread it to your friends!

magento-2-tutorial
how-to
new-total
email

Module Development

Comments for How to Add New Total to Email in Magento 2

Please leave comments if you have any questions, feedbacks.

You also may like these Magento 2 Extensions

One Step Checkout

$199
10 reviews

Layered Navigation

$99
12 reviews

SEO

FREE
no review

Shop By Brand

$99
3 reviews

People also searched for:

  • magento 2 add new total block
  • magento 2 email
  • magento 2 new total
  • email in magento 2
  • new total in magneto 2
  • magento 2 add fee or discount to order totals
  • magento add extra fee in checkout
  • magento add fee or discount to order totals
  • magento 2 add extra fee in checkout
  • magento add custom fee
  • magento add discount programmatically
  • magento add fee to order
  • magento extra fee extension