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:
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:
-
-
- Create registration.php file at app\code\Vendor\Module directory
12345<?phpuse \Magento\Framework\Component\ComponentRegistrar;ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Vendor_Module', __DIR__); - Create module.xml file at app\code\Vendor\Module\etc directory
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_new.xml file at app\code\Vendor\Module\view\adminhtml\layout
123456789<?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> - Create Qty.php file at app\code\Vendor\Module\Block\Sales\Order
12345678910111213141516171819202122232425262728293031323334353637<?phpnamespace 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();}}
- Create registration.php file at app\code\Vendor\Module directory
-
Once done, the quantity is listed under “Invoice Totals” as shown here:
Improve the invoice PDF with the above solution in Magento 2 store.
Any doubts about the method can be mentioned in the Comments section below. I’d be happy to help you out.
Also, do share the solution 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.
6 Comments
How to we can add product image in invoice pdf and email template ?
Hello Wajahat,
We will post the solution in the future.
Do subscribe to Meetanshi’s blog posts to get notified for the same.
Thank You
Hi, this is a nice solution. The only thing is that I get the notification that the block is invalid. (Using Magento 2.2.4). Do you know what might be the problem?
Hello,
Thank you for the appreciation,
Please check the path of block or name of the class whether it’s proper or not, there might be some mistake.
Thank You
Hi Sanjay,
I have checked everything but I’m still getting the “Invalid Block type: VendorModuleBlockSalesOrderQty” error.
I wonder what could be the problem. Just in order to avoid errors in typing I have used the exact naming as in the above (namespaces and directories).
What am I missing here? Could you advise me?
Thanks in advance
Hello,
Check out and implement the updated code of the Qty.php file.
Thank You.