Change Magento 2 Increment Id
This post is about how to change Magento 2 increment id for your orders and other documents. It is quite complicated, but there are more options to customize your numbers. I show you all that you need to know.
Change Magento 2 Increment Id
Changing Magento 1 increment id is quite easy, but has only few options to customize your ids. It can only be an ascending number. Magento 2 offers you a far more complex method to individualize your order increment ids. The formula for creating a new increment id looks as follows:
1 | increment_id = prefix + ((sequence_value – start_value) * step + start_value) {padded to X digits} + suffix |
Details about this can be found in Magento\SalesSequence\Model\Sequence. It looks quite complicated, but it is very simple. The next increment_id is a string that starts with a prefix, a 9 digit number (with leading 0s) and a suffix. The 9 digit number is incremented and default the same as order id. suffix and prefix are default an empty string. That means your first order will have the increment id 000000001.
Prefix and suffix
You can set prefix and suffix in your Magento 2 database. There is currently no option in your Magento 2 adminhtml for this. Table sales_sequence_profile lists your increment ids by store and type and shows you the following data:
Here you can set prefix or/and suffix for each individual store. If you want to know if a specific line is an order id, shipment id or anything else, you have to compare meta_id to sales_sequence_meta table. This table lists all types for each meta_id as you can see:
Start_value, max_value and step
In your sales_sequence_profile table you can define more than suffix and prefix. You are free to set another start_value for first generated id (please be aware, that this only affects first generated id). There is also a max_value and a warning_value if you want to get informed if you run out of free ids. This is minor important, because the numbers are very high. The most interesting column is step. You can define another increment step than 1, which means each new generated id is incremented for example by 2 or by 10. I don’t know why you should set another value than 1, but it is possible.
This is basically all you need to set and define your incremend ids for your Magento 2 shop, but there is one more thing to know: sequence tables!
Sequence tables
If you are running a Magento 2 multi-store, you may wondered about sequence tables that are flooding your database. At first glance it looks like a hack or some sort of error. Your database is full of generic tables as you can see on the following screenshot:
These tables keep track of last generated increment id values, so that Magento 2 knows, which number was the last one. Tables are generated for each object type that uses increment ids (order , credit memo, shipment, …) and for each store view. On a big multi-store setup there can be dozen of tables.
Conclusion
Magento 2 offers you much more possibilities to change increment id values. I don’t know if this is a good idea, because some people tend to be too creative with such important numbers (important for tax and accounting). If you need to do special formatted numbers (because an external system needs this or your accountant can’t work with other numbers) you are free to configure it as you want. The rules are quite simple to do complex increment ids that increment automatically and correct.