How to Add Custom Field in Invoice Totals in Magento 2 Invoice Email
An invoice is an official document that includes a detailed description of a purchase, quantity, price, shipping cost, sales tax, total, etc.
Magento 2 allows configuring invoice emails to send the customers a detailed invoice when they place an order and complete the payments.
Sending an invoice through email helps enhance your store’s brand identity as well as makes the whole transaction legal and reliable. Moreover, it’s an interactive way to boost the user experience and avoid any legal issues in the future.
The default Magento 2 offers multiple options in the invoice email. However, what if you want to add a custom field in the invoice email?
For instance, you’ve added extra charges on the packaging or product personalization. Or even offered a discount based on the store credit value. Now, the total amount is calculated considering these additional charges or discounts.
However, the email invoice does not add custom charges by default.
In such a case, it can create confusion for the customers if the grand total in the invoice email does not match the total amount they paid.
So, in order to display the manually added charges, one has to follow the below method to add custom field in invoice totals in Magento 2 invoice email.
Method to Add Custom Field in Invoice Totals in Magento 2 Invoice Email
- Create registration.php file at app\code\Vendor\Module and use the below code.
123456<?php\Magento\Framework\Component\ComponentRegistrar::register(\Magento\Framework\Component\ComponentRegistrar::MODULE,'Vendor_Module', __DIR__); - Create module.xml file at app\code\Vendor\Module\etc
12345<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"><module name="Vendor_Module" setup_version="1.0.0"/></config> - Create sales_order_invoice.xml file at app\code\Vendor\Module\view\frontend\layout
12345678910111213<?xml version="1.0"?><page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"><body><referenceBlock name="invoice_totals"><block class="Vendor\Module\Block\Total" name="invoice"/><action method="setLabel"><argument name="label" xsi:type="string">Order Packing Charges</argument></action></referenceBlock></body></page> - Create Total.php file at app\code\Vendor\Module\Block
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051<?phpnamespace Vendor\Module\Block;use Magento\Framework\View\Element\Template;class Total extends Template{public function __construct(Template\Context $context,array $data = []){parent::__construct($context, $data);$this->setInitialFields();}public function setInitialFields(){if (!$this->getLabel()) {$this->setLabel(__('Order Packing Charges'));}}public function initTotals(){$this->getParentBlock()->addTotal(new \Magento\Framework\DataObject(['code' => 'custom','strong' => $this->getStrong(),'value' => $this->getOrder()->getCustom(), // extension attribute field'base_value' => $this->getOrder()->getCustom(),'label' => __($this->getLabel()),]),$this->getAfter());return $this;}public function getOrder(){return $this->getParentBlock()->getOrder();}public function getSource(){return $this->getParentBlock()->getSource();}}
The invoice mail will look something like this:
That’s it. Likewise you can also add custom field below apply discount code in Magento 2, to make customers click on it and have more detailed offer benefits. Also with increasing E-commerce facilities, the default features fall short. Fortunately, Magento 2 allows customizing the default features, for example, generate invoice with pending status in Magento 2.
If you have queries, feel free to ask in the Comments section below.
I would like to solve your problem.
Don’t forget to share this post with Magento Community via social media.
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
1 Comment
thank you dear