{"id":1578,"date":"2021-02-10T13:06:28","date_gmt":"2021-02-10T13:06:28","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/change-sequence-of-custom-field-before-subtotal-in-invoice-in-magento-2-backend\/"},"modified":"2025-07-21T15:38:04","modified_gmt":"2025-07-21T10:08:04","slug":"change-sequence-of-custom-field-before-subtotal-in-invoice-in-magento-2-backend","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/change-sequence-of-custom-field-before-subtotal-in-invoice-in-magento-2-backend\/","title":{"rendered":"How to Change the Sequence of Custom Field Before &#8220;subtotal&#8221; in Invoice in Magento 2 Backend"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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\u2019 trust and shopping experience.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The default&nbsp;Magento 2&nbsp;offers an invoice template as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2021\/01\/default-OP.png\" alt=\"Default Magento output\" class=\"wp-image-12958\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The admin may want to add custom fields and change the sequence of the existing fields as per the business requirements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here, I have taken the example to&nbsp;<em><strong>c<\/strong><strong>hange the sequence of custom field before \u201csubtotal\u201d in&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/auto-generate-invoices-and-shipments-for-backend-orders-in-magento-2\/\">invoice in Magento 2 backend<\/a><\/strong><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, the \u201cqty\u201d custom field is added and the sequence of \u201csubtotal\u201d changes as \u201cqty\u201d is placed in the first position.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Change the Sequence of Custom Field Before \u201csubtotal\u201d in Invoice in Magento 2 Backend:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create&nbsp;<strong>registration.php<\/strong>&nbsp;file at&nbsp;<strong><strong><strong>app\\code\\Vendor\\Module<\/strong><\/strong><\/strong>&nbsp;directory<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nuse Magento\\Framework\\Component\\ComponentRegistrar;\nComponentRegistrar::register(ComponentRegistrar::MODULE, 'Vendor_Module', __DIR__);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create&nbsp;<strong>module.xml<\/strong>&nbsp;file at&nbsp;<strong><strong><strong><strong>app\\code\\Vendor\\Module\\etc<\/strong><\/strong><\/strong><\/strong> <strong>directory<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\">\n    &lt;module name=\"Vendor_Module\" setup_version=\"1.0.0\"\/>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Create&nbsp;<strong>sales_order_invoice_new.xml<\/strong>&nbsp;file at&nbsp;<strong><strong>app\\code\\Vendor\\Module\\view\\adminhtml\\layout<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;page xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n      xsi:noNamespaceSchemaLocation=\"urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd\">\n    &lt;body>\n        &lt;referenceBlock name=\"invoice_totals\">\n            &lt;block class=\"Vendor\\Module\\Block\\SalesOrder\\Qty\" name=\"qty\"\/>\n        &lt;\/referenceBlock>\n    &lt;\/body>\n&lt;\/page><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">4. Create&nbsp;<strong>Qty.php<\/strong>&nbsp;file at&nbsp;<strong><strong>app\\code\\Vendor\\Module\\BlockSalesOrder<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace Vendor\\Module\\Block\\SalesOrder;\n\nuse Magento\\Framework\\View\\Element\\Template;\nuse Magento\\Framework\\View\\Element\\Template\\Context;\nuse Magento\\Framework\\Pricing\\PriceCurrencyInterface as Currency;\nuse Magento\\Framework\\DataObject;\n\nclass Qty extends Template\n{\n    protected $currency;\n\n    public function __construct(Context $context, Currency $currency, array $data = [])\n    {\n        parent::__construct($context, $data);\n        $this->currency = $currency;\n    }\n\n    public function initTotals()\n    {\n        if ((integer) $this->getOrder()->getTotalQtyOrdered()) {\n            $value = $this->getOrder()->getTotalQtyOrdered();\n            $this->getParentBlock()->addTotalBefore(\n                new DataObject([\n                    'code' => 'qty',\n                    'strong' => false,\n                    'label' => __('QTY'),\n                    'value' => $value,\n                ]),\n                'subtotal'\n            );\n        }\n        return $this;\n    }\n\n    public function getOrder()\n    {\n        return $this->getParentBlock()->getOrder();\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Using the above code, the QTY field is added to the invoice before subtotal as shown in below image.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2021\/01\/Custom-1.png\" alt=\"QTY field\" class=\"wp-image-12962\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s all!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to share the solution with Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank You.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Related Posts:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"https:\/\/meetanshi.com\/blog\/create-barcode-and-add-it-in-magento-2-invoice-pdf\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Create Barcode and Add it in Magento 2 Invoice PDF<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/meetanshi.com\/blog\/add-order-id-in-invoice-pdf-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Add Order In Invoice PDF in Magento 2<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/meetanshi.com\/blog\/change-pdf-invoice-logo-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Change PDF Invoice Logo in Magento 2<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/meetanshi.com\/blog\/add-header-and-footer-to-magento-2-invoice-pdf\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Add Header and Footer to Magento 2 Invoice PDF<\/a><\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1578","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1578","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1578"}],"version-history":[{"count":7,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1578\/revisions"}],"predecessor-version":[{"id":18768,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1578\/revisions\/18768"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}