How to Change Increment Ids Length

A quite often request that is coming from time to time is to change the length of the increment id used for the sales entities, like order, invoice, creditmemo and shipment. In this post we will see together how you can adjust it to the length that you need!

By default magento has a default pattern on which all increment Ids are based. This pattern is defined as a const in the class Magento\SalesSequence\Mdel\Secequence and looks like the one below.

const DEFAULT_PATTERN = "%s%'.09d%s";

Meaning that the increment ids will be consisted of a string + 9 digits + a string. In magento the strings are the prefix and suffix that an increment id can have. These string values can be defined directly in the database and more precisely in the sales_sequence_profile table. These values are specified for every sales entity type and store. So for example in a default magento installation the first order, invoice, creditmemo and shipment will take the increment id 000000001. If you are requested now to change that length to 5 digits for example, you can simple have to add the following code in the di.xml of your custom module.

So now the next order, invoice, creditmemo and shipment will take the increment id 00002. Please notice that with the code above you change the pattern for all increments ids and not only for orders.

Feel free to share this post and ask your questions in the comments below.

Till next time!

DigitalOcean Referral Badge