How to Add Quantity Field in Invoice Total in Magento 2 Admin Panel

Magento 2 store owners are responsible for maintaining the professional standards and delightful customer experience to stay ahead of the competition.

As a part of it, updating customers about their order details, shipping status, invoice documents, credit memo, etc. is important.

An invoice is a legal document that reflects the transaction between buyer and seller. For E-commerce store owners, an invoice must be as detailed as possible that covers essential information a customer must know.

The default Magento 2 invoice includes details like subtotal, shipping and handling charges, tax, and grand total under “Invoice Totals” as shown here:

Default Invoice Total in Magento 2 Admin Panel

However, one may want to add quantity field in invoice total in Magento 2 admin panel which is an important detail to be included in the invoice PDF.

The below programmatic solution allows to do so!

Method to Add Quantity Field in Invoice Total in Magento 2 Admin Panel:

1. Create registration.php file at app\code\Vendor\Module directory

<?php

use \Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Vendor_Module', __DIR__);

2. Create module.xml file at app\code\Vendor\Module\etc directory

<?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>

3. Create sales_order_invoice_new.xml file at app\code\Vendor\Module\view\adminhtml\layout

<?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\Sales\Order\Qty" name="qty"/>
        </referenceBlock>
    </body>
</page>

4. Create Qty.php file at app\code\Vendor\Module\Block\Sales\Order

<?php

namespace Vendor\Module\Block\Sales\Order;

use Magento\Framework\View\Element\Template\Context;

class Qty extends \Magento\Framework\View\Element\Template
{
    public function __construct(Context $context, array $data = [])
    {
        parent::__construct($context, $data);
    }

    public function initTotals()
    {
        if ((double)$this->getOrder()->getTotalQtyOrdered()) {
            $value = $this->getOrder()->getTotalQtyOrdered();

            $this->getParentBlock()->addTotal(
                new \Magento\Framework\DataObject(
                    [
                        'code' => 'qty',
                        'strong' => false,
                        'label' => 'QTY',
                        'value' => $value,
                    ]
                )
            );
        }
        return $this;
    }

    public function getOrder()
    {
        return $this->getParentBlock()->getOrder();
    }
}

Once done, the quantity is listed under “Invoice Totals” as shown here:

Add Quantity Field in Invoice Total in Magento 2 Admin Panel

Improve the invoice PDF with the above solution in Magento 2 store.

In order to display the manually added charges, one has to add custom field in invoice totals in Magento 2 invoice email

Also, do share the solution with Magento Community via social media.

Thank you.

Sanjay Jethva

Article by

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...