How To

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

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.

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="Vendor\Extension\Block\CustomFieldinInvoice" name="invoice"/>
            <action method="setLabel">
                <argument name="label" xsi:type="string">Custom Field Name</argument>
            </action>
        </referenceBlock>
    </body>
</page>

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 Vendor\Extension\Block;
use Magento\Framework\View\Element\Template;
 
class CustomFieldinInvoice extends Template
{
    public function __construct(
        Template\Context $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 \Magento\Framework\DataObject(
                [
                    '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.

Happy Coding!

Click to rate this post!
[Total: 3 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

6 days ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

1 week ago

Top 10 Tips to Hire Shopify Developers

As the world of eCommerce continues to thrive, Shopify has become one of the most…

2 weeks ago

Managing Browser Events and Navigation in Shopify Remix: useBeforeUnload, useHref, and useLocation Hooks

Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…

2 weeks ago

Ultimate Guide to Hiring a Top Shopify Development Agency

Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…

2 weeks ago