---
title: "How to Add Custom Fields in Invoice Totals in Magento 2 Invoice Email Programmatically?"
url: "https://magecomp.com/blog/add-custom-fields-in-invoice-totals-in-magento-2-invoice-email-programmatically/"
date: "2022-11-18T16:01:26+00:00"
modified: "2025-02-13T12:33:09+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 403
reading_time: "3 min read"
summary: "Hello Magento Friends,"
description: "Invoice totals may require additional information to be added. Learn How to Add Custom Fields in Invoice Totals in Magento 2 Invoice Email"
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "Magento 2: How To Display Count Of Product with Page Title on Category Page "
    url: "https://magecomp.com/blog/magento-2-display-count-of-product-with-page-title-on-category-page/"
  - title: "How to Create Custom Payment Method in Magento 2?"
    url: "https://magecomp.com/blog/create-custom-payment-method-in-magento-2/"
  - title: "How to Get Invoice Details by Invoice Id in Magento 2"
    url: "https://magecomp.com/blog/get-invoice-details-by-invoice-id-magento-2/"
---

# How to Add Custom Fields in Invoice Totals in Magento 2 Invoice Email Programmatically?

_Published: November 18, 2022_  
_Author: Dhiren Vasoya_  

![How to Add Custom Fields in Invoice Totals in Magento 2 Invoice Email Programmatically](https://magecomp.com/blog/wp-content/uploads/2022/11/How-to-Add-Custom-Fields-in-Invoice-Totals-in-Magento-2-Invoice-Email-Programmatically-1024x512.webp)

**Hello Magento Friends,**

In today’s tutorial, we will learn about How to Add Custom Fields in Invoice Totals in Magento 2 Invoice Email Programmatically?
An admin sends an invoice to customers when the order is placed. Invoices can be generated in the form of PDFs or sent to customers via email. Automatize the invoice process of your Magento 2 store by installing [**Magento 2 Auto Invoice & Shipment**](https://magecomp.com/magento-2-auto-invoice-and-shipment.html).
[![Hire Magento Programmer](https://magecomp.com/blog/wp-content/uploads/2025/02/Hire-Magento-Programmer-CTA-2.webp)](https://magecomp.com/services/hire-magento-programmer/)

Admin can configure invoice email in Magento 2. The invoice email contains order totals and other information. If you want to show custom fields in invoice totals, follow the steps below.
## Steps to Add Custom Fields in Invoice Totals in Magento 2 Invoice Email Programmatically:
**Step 1:** In the first step you need to create the **sales_order_invoice.xml** file at the below file path
**app/code/Vendor/Extension/view/frontend/layout/sales_order_invoice.xml**

Then add the code as follows
```
<?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="VendorExtensionBlockCustomFieldinInvoice" name="invoice"/>
            <action method="setLabel">
                <argument name="label" xsi:type="string">Custom Field Name</argument>
            </action>
        </referenceBlock>
    </body>
</page>
```

[![Magento version upgrade](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Version-Upgrade-CTA-3.webp)](https://magecomp.com/services/magento-upgrade-services/)

**Step 2:** Now create the **CustomFieldinInvoice.php** file at the following path
**app/code/Vendor/Extension/Block/CustomFieldinInvoice.php**

Now add the below code
```
<?php

namespace VendorExtensionBlock;
use MagentoFrameworkViewElementTemplate;

class CustomFieldinInvoice extends Template
{
    public function __construct(
        TemplateContext $context,
        array $data = []
    )
    {
        parent::__construct($context, $data);
        $this->setInitialFields();
    }

    public function setInitialFields()
    {
        if (!$this->getLabel())
        {
            $this->setLabel(__('Custom Field Name'));
        }
    }

    public function initTotals()
    {
        $this->getParentBlock()->addTotal(
            new MagentoFrameworkDataObject(
                [
                    'code' => 'customfield',
                    '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();
    }
}
```

## Conclusion:
Hence, this way, you can easily add custom fields in invoice totals in Magento 2 invoice email programmatically. If you have any doubts, let me know through the comment box. Share the article with your friends and stay in touch with us.
[![Magento Malware Removal](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-Malware-Removal-CTA-6.webp)](https://magecomp.com/magento-malware-removal.html)

**Happy Coding!**

</body>


---

_View the original post at: [https://magecomp.com/blog/add-custom-fields-in-invoice-totals-in-magento-2-invoice-email-programmatically/](https://magecomp.com/blog/add-custom-fields-in-invoice-totals-in-magento-2-invoice-email-programmatically/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-09-08 12:01:08 UTC_  
