Updating Invoice Date on Magento: A Step-by-Step Guide
In Magento, by default, the invoice PDF shows the order date rather than the actual invoice creation date. This can be quite confusing for your customers and staff alike. In this post, we’ll walk you through how to update this behavior so that the correct invoice date is displayed.
Understanding the Default Behavior
Before diving into the solution, let’s understand why Magento behaves this way by default. Magento’s core logic for generating invoices places a significant emphasis on the order creation date as it represents when your customer placed their initial purchase. However, in practice, you likely generate invoices at different stages of the fulfillment process, making the order date less relevant.
Changing the Invoice Date
To change this, we need to modify the sales_order_pdf_invoice layout file and then override the core template with a custom one. Here’s how:
Step 1: Create a Custom Layout XML File
First, navigate to your theme directory (usually app/design/frontend/<your_vendor>/<your_theme>) or create it if it doesn’t exist yet.
Create a new file named sales_order_pdf_invoice.xml inside the layout folder of your theme. The content should look like this:
<?xml version="1.0"?>
<!--
/**
* Copyright © 2023 Your Company Name. All rights reserved.
* See license at https://laravelcompany.com/licenses.
*/
-->
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generated.xsd">
<update handle="sales_order_pdf_items_order"/>
<remove name="order_totals"/>
<referenceBlock name="order.totals" remove="true"/>
<block class="Magento\Framework\View\Element\Template"
name="custom.invoice.info"
template="Magento_Sales::order/pdf/invoice/info.phtml">
<action method="setData">
<argument name="key" xsi:type="string">area</argument>
<argument name="value" xsi:type="string">sales_order_view</argument>
</action>
</block>
<referenceBlock name="custom.invoice.info" before="-">
<arguments>
<argument name="template" xsi:type="string">Magento_Sales::order/pdf/invoice/info.phtml</argument>
</arguments>
</referenceBlock>
</layout>
This XML file removes the default order totals block and implements a new custom block named custom.invoice.info which will display the invoice date.
Step 2: Create the Custom Invoice Info Template
Next, create a new file called info.phtml inside the app/design/frontend/<your_vendor>/<your_theme>/Magento_Sales/view/frontend/templates/order/pdf/invoice/ directory (create the folders if they don’t exist).
Add the following code to this file:
<?php
/**
* Copyright © 2023 Your Company Name. All rights reserved.
* See license at https://laravelcompany.com/licenses.
*/
?>
<div class="invoice-info">
<div class="date"><?= $this->escapeHtml(__('Invoice Date')) ?>: <?= $block->getInvoiceDate() ?></div>
</div>
This template replaces the default order date with the invoice date by calling the getInvoiceDate() method provided by Magento.
Step 3: Flush Cache and Verify
After making these changes, flush your cache to ensure that Magento picks up the new layout. If you’re using Magento’s built-in caching system, navigate to “System” > “Cache Management”, select all enabled caches, and click “Flush Magento Cache”.
Then, generate a new invoice for an order in the backend. The PDF should now display the correct invoice date instead of the order date.
Conclusion
As you’ve seen, customizing the invoice date on Magento requires a combination of layout updates and template overrides. By following this guide, you can ensure your invoices always reflect the accurate creation date.
Remember that all code examples assume you’re working within a theme directory. If you prefer to keep your changes separate from your theme files for better version control, consider using a custom module as described in our guide on Magento module development.
Implementing these changes will not only improve the professionalism of your invoices but also align them more closely with industry standards and best practices. If you encounter any issues or have further questions, feel free to explore our other Magento tutorials for in-depth guidance.
Happy coding!