Magento - custom invoice number

Magento - custom invoice number

July 26, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - custom invoice number

Understanding Magento Invoice Number Format

When it comes to customizing invoice numbers in Magento, you’re not alone in needing more control over your numbering system. By default, Magento generates invoice numbers sequentially starting from 100000001. However, sometimes business requirements necessitate a different format or prefix.

The anatomy of the default Magento invoice number

The default invoice number is generated using the increment_last_id value stored in the eav_entity_store table. This ID is incremented each time an invoice is created. To change the starting point to 2012 - 00001, you would need to set this field appropriately.

Customizing your invoice number format

To achieve your desired invoice number format, you have two primary approaches:

Method 1: Directly editing the database value

You can directly update the increment_last_id in the eav_entity_store table. Here’s an example of how to do this using MySQL:

UPDATE eav_entity_store SET increment_last_id = '2012-00000' WHERE entity_type_id = 4;

Please ensure you have a backup before making any direct database changes.

Method 2: Using Magento’s built-in features

A more robust approach is to use Magento’s event observers and the sales_order_invoice model. This allows you to modify the invoice number without directly manipulating the database.

First, create an observer for the sales_order_invoice_save_before event:

class CustomInvoiceNumberObserver implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $invoice = $observer->getDataObject();
        $number = '2012 - '.str_pad($invoice->getId(), 5, '0', STR_PAD_LEFT);
        $invoice->setNumber($number);
    }
}

Then register this observer in your module configuration:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Your_Module" setup_version="3.4.1">
        <sequence>
            <module name="Magento_Sales"/>
        </sequence>
    </module>
</config>

Best practices for customizing invoice numbers

When customizing invoice numbers, it’s crucial to consider the following:

  • Ensure your new number format is unique and does not conflict with existing invoices or other document types.
  • If you’re using a prefix like 2012 -, be mindful of how this will look when printed on order documents.
  • Always backup your database before making any changes.

Conclusion

Customizing invoice numbers in Magento requires an understanding of the underlying data structure and events system. By directly editing the increment_last_id or using observers, you can achieve your desired invoice number format.

Remember, while direct database edits are simpler, they carry more risk. Using Magento’s built-in features provides a more maintainable and upgrade-friendly solution.

For further reading on advanced Magento customizations and best practices, I recommend checking out our expert guides at LaravelCompany.

Happy coding!

Database Extensions Orders Errors

Our network