How to change the default order increment ID in Magento 2?

Say, you want to change the Magento order increment ID in your Magento 2 store with a custom one. You can do this in 2 ways:

Let’s consider them both.

Method 1. Change increment ID programmatically

Warning: The code provided below is illustrative. You need to customize it for your specific Magento 2 installation.

Step 1. Register a new module by creating a file app/code/Amasty/Testgrid/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Amasty_Testgrid',
__DIR__
);

Step 2. Next, you need to create the module.xml file in the app/code/Amasty/Testgrid/etc folder to define installation and update functions.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Amasty_Testgrid" setup_version="1.0.0">
</module>
</config>

Step 3. Then, create app/code/Amasty/Testgrid/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\DB\Sequence\SequenceInterface">
<arguments>
<argument name="pattern" xsi:type="string">ABC%s%'.06d%sDFG</argument>
</arguments>
</type>
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
<arguments>
<argument name="collections" xsi:type="array">
<item name="sales_order_grid_data_source"
xsi:type="string">Amasty\Testgrid\Model\ResourceModel\Order\Grid\Collection</item>
</argument>
</arguments>
</type>
<type name="Amasty\Testgrid\Model\ResourceModel\Order\Grid\Collection">
<arguments>
<argument name="mainTable" xsi:type="string">sales_order_grid</argument>
<argument name="resourceModel"
xsi:type="string">Magento\Sales\Model\ResourceModel\Order</argument>
</arguments>
</type>
</config>

And change the pattern from "%s%'.09d%s" to “ABC%s%'.06d%sDFG”. In this case, ABC is a Magento 2 order increment ID prefix, and DFG is postfix. They will change the Magento 2 default order number. Also, in our example, we reduced the number of digits from 9 to 6.

If you want to start order numeration from a specific digit, check the official documentation to see how to do it.

Method 2. Change increment ID via the Magento 2 Custom Order Number plugin

In Magento 2 to load order by increment ID, you can use extensions. The Custom Order Number extension allows you to get orders by increment id in Magento 2 and manage increment ID the way you want. You can add prefix, postfix, change the counter increment step and padding. It supports different variables like:

  • {store} for store name;
  • {yyyy} for a year;
  • {mm} for a month;
  • {dd} for a day;
  • {country_code} for a country code.

You can use them to specify number format. With this extension, you can change Order, Invoice, Shipping, and Credit Memo data. Moreover, you can set different counters for each website and store view. 

How can we help you?

Didn’t you find the answer to your question? We are always happy to help you out.

© 2009-2024 Amasty. All Rights Reserved.