How to Add New Total to Email in Magento 2

In this article, we will find 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 in 2 Steps

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.

Wrap up

So this is all you need to add a New Total to Email in Magento 2. Perform all these steps carefully and double-check them to ensure the results come out successfully.

Thank you for reading!

Enjoyed the tutorial? Spread it to your friends!

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

Sam Thomas
Sam Thomas

CEO and Founder of Mageplaza. Pursueing a simple and healthy lifestyle. A friend, a husband and a dad of two children, a trainer and an influencer wannabe. He is a big fan of sports and travel, also.

People also searched for

  • magento 2 add new total block
  • magento 2 email
  • magento 2 new total
  • email in magento 2
  • new total in Magento 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
  • 2.2.x, 2.3.x, 2.4.x