{"id":1778,"date":"2021-05-20T06:23:01","date_gmt":"2021-05-20T06:23:01","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/add-custom-field-in-invoice-totals-in-magento-2-invoice-email\/"},"modified":"2025-07-16T17:48:19","modified_gmt":"2025-07-16T12:18:19","slug":"add-custom-field-in-invoice-totals-in-magento-2-invoice-email","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/add-custom-field-in-invoice-totals-in-magento-2-invoice-email\/","title":{"rendered":"How to Add Custom Field in Invoice Totals in Magento 2 Invoice Email"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">An&nbsp;invoice&nbsp;is an official document that includes a detailed description of a purchase, quantity, price, shipping cost, sales tax, total, etc.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/meetanshi.com\/blog\/configure-invoice-emails-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 allows configuring invoice emails<\/a>&nbsp;to send the customers a detailed invoice when they place an order and complete the payments.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Sending an invoice through email helps enhance your store\u2019s brand identity as well as makes the whole transaction legal and reliable. Moreover, it\u2019s an interactive way to boost the user experience and avoid any legal issues in the future.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The default\u00a0Magento 2\u00a0offers multiple options in the invoice email. However, what if you want to add a custom field in the invoice email?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For instance, you\u2019ve addappcodeVendorModuleBlocked 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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, the email invoice does not add custom charges by default.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, in order to display the manually added charges, one has to follow the below method to&nbsp;<em><strong>add custom field in invoice totals in Magento 2 invoice email<\/strong><\/em>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Add Custom Field in Invoice Totals in Magento 2 Invoice Email<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create&nbsp;<strong>registration.php<\/strong>&nbsp;file at&nbsp;<strong><strong>app\\code\\Vendor\\Module<\/strong>&nbsp;<\/strong>and use the below code.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\n\\Magento\\Framework\\Component\\ComponentRegistrar::register(\n    \\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,\n    'Vendor_Module', __DIR__\n);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create&nbsp;<strong>module.xml<\/strong>&nbsp;file at&nbsp;<strong><strong><strong><strong>app\\code\\Vendor\\Module\\etc<\/strong><\/strong><\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\">\n    &lt;module name=\"Vendor_Module\" setup_version=\"1.0.0\"\/>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Create&nbsp;<strong>sales_order_invoice.xml<\/strong>&nbsp;file at&nbsp;<strong><strong><strong><strong>app\\code\\Vendor\\Module\\view\\frontend\\layout<\/strong><\/strong><\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n\n&lt;page xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n      xsi:noNamespaceSchemaLocation=\"urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd\">\n    &lt;body>\n        &lt;referenceBlock name=\"invoice_totals\">\n            &lt;block class=\"Vendor\\Module\\Block\\Total\" name=\"invoice\"\/>\n            &lt;action method=\"setLabel\">\n                &lt;argument name=\"label\" xsi:type=\"string\">Order Packing Charges&lt;\/argument>\n            &lt;\/action>\n        &lt;\/referenceBlock>\n    &lt;\/body>\n&lt;\/page><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">4. Create&nbsp;<strong>Total.php<\/strong>&nbsp;file at&nbsp;<strong><strong><strong><strong>app\\code\\Vendor\\Module\\Block<\/strong><\/strong><\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Module\\Block;\n\nuse Magento\\Framework\\View\\Element\\Template;\n\nclass Total extends Template\n{\n    public function __construct(\n        Template\\Context $context,\n        array $data = []\n    )\n    {\n        parent::__construct($context, $data);\n        $this->setInitialFields();\n    }\n\n    public function setInitialFields()\n    {\n        if (!$this->getLabel()) {\n            $this->setLabel(__('Order Packing Charges'));\n        }\n    }\n\n    public function initTotals()\n    {\n        $this->getParentBlock()->addTotal(\n            new \\Magento\\Framework\\DataObject(\n                [\n                    'code' => 'custom',\n                    'strong' => $this->getStrong(),\n                    'value' => $this->getOrder()->getCustom(), \/\/ extension attribute field\n                    'base_value' => $this->getOrder()->getCustom(),\n                    'label' => __($this->getLabel()),\n                ]\n            ),\n            $this->getAfter()\n        );\n        return $this;\n    }\n\n    public function getOrder()\n    {\n        return $this->getParentBlock()->getOrder();\n    }\n\n    public function getSource()\n    {\n        return $this->getParentBlock()->getSource();\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The invoice mail will look something like this:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2021\/05\/Screenshot-at-May-13th-2021-11.42.28-am-1.png\" alt=\"How to Add Custom Field in Invoice Totals in Magento 2 Invoice Email\" class=\"wp-image-15032\" style=\"width:652px;height:auto\"\/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it. Likewise you can also&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/add-custom-field-below-apply-discount-code-in-magento-2-cart-page\/\">add custom field below apply discount code in Magento 2<\/a>, 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,&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/generate-invoice-with-pending-status-in-magento-2\/\">generate invoice with pending status in Magento 2.<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Don\u2019t forget to share this post with Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An&nbsp;invoice&nbsp;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&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1778","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1778","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1778"}],"version-history":[{"count":7,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1778\/revisions"}],"predecessor-version":[{"id":18070,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1778\/revisions\/18070"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}