Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

How to Add New Total to Email in Magento 2

Vinh Jacker | 12-18-2024

Add New Total to Email

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!

x
    Jacker

    With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services