How to Change the Sequence of Custom Field Before “subtotal” in Invoice in Magento 2 Backend
An invoice is the legal accounting document that shows the transaction details between a merchant and a customer. Businesses need to craft the invoice design meticulously as it represents the brand and contributes to customers’ trust and shopping experience.
The Magento 2 store admin has to generate invoices for orders placed online. The invoice layout in backend must also be such that helps admin manage the order details easily.
The default Magento 2 offers an invoice template as shown below.
The admin may want to add custom fields and change the sequence of the existing fields as per the business requirements.
Here, I have taken the example to change the sequence of custom field before “subtotal” in invoice in Magento 2 backend.
In this example, the “qty” custom field is added and the sequence of “subtotal” changes as “qty” is placed in the first position.
Such changes in Magento 2 admin panel for invoice helps admin to get all the required details in invoice totals in the sequence of priority.
Steps to Change the Sequence of Custom Field Before “subtotal” in Invoice in Magento 2 Backend:
- Create registration.php file at appcodeVendorModule directory
12345<?phpuse MagentoFrameworkComponentComponentRegistrar;ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Vendor_Module', __DIR__); - Create module.xml file at appcodeVendorModuleetc 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 appcodeVendorModuleviewadminhtmllayout
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="VendorModuleBlockSalesOrderQty" name="qty"/></referenceBlock></body></page> - Create Qty.php file at appcodeVendorModuleBlockSalesOrder
123456789101112131415161718192021222324252627282930313233343536373839404142<?phpnamespace VendorModuleBlockSalesOrder;use MagentoFrameworkViewElementTemplateContext;class Qty extends MagentoFrameworkViewElementTemplate{protected $currency;public function __construct(Context $context, Currency $currency, array $data = []){parent::__construct($context, $data);$this->currency = $currency;}public function initTotals(){if ((integer)$this->getOrder()->getTotalQtyOrdered()) {$value = $this->getOrder()->getTotalQtyOrdered();$this->getParentBlock()->addTotalBefore(new MagentoFrameworkDataObject(['code' => 'qty','strong' => false,'label' => 'QTY','value' => $value,]),'subtotal');}return $this;}public function getOrder(){return $this->getParentBlock()->getOrder();}}
Using the above code, the QTY field is added to the invoice before subtotal as shown in below image.
That’s all!
If you have any doubts regarding this post, do mention them in the Comments section below.
I would be happy to help.
Feel free to share the solution with Magento Community via social media.
Thank You.
Related Posts:
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.
Prev
How To Get All Category URLs in Magento 2
How To Override Product NewWidget Block in Magento 2
Next