Today, we’re going to teach you guys how to change the default order increment ID programmatically in a Magento 2 store.
Now, there are two different ways to change the default order increment ID in Magento 2. - Programmatically by creating a custom plugin & integrating a custom order number plugin.
Below, we will learn to change the default order increment ID by programming a custom plugin.
That being said, let’s jump into it!
Steps to Change the Default Order Increment ID in Magento 2 (Programmatically)
Please follow the below steps to learn how to change the default order increment ID in your Magento 2 store.
Step 1.
First of all, you need to register a new module by creating a registration.php file in the app/code/MageDelight/Testgrid/ folder and paste the following code.
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'MageDelight_Testgrid', __DIR__ );
Step 2.
After that, you need to create a module.xml file to define installation and update functions in the app/code/MageDelight/Testgrid/etc folder and paste the following code.
<?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="MageDelight_Testgrid" setup_version="1.0.0"> </module> </config>
Step 3.
Next, you need to create a di.xml file in the app/code/MageDelight/Testgrid/etc/ folder and paste the following code.
<?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">MageDelight\Testgrid\Model\ResourceModel\Order\Grid\Collection</item> </argument> </arguments> </type> <type name="MageDelight\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>
Step 4.
Lastly, you need to change the pattern from %s%'.09d%s to ABC%s%'.06d%sDFG, and it will change the default Magento 2 order number.
Conclusion
And that’s about it!
This is how you can change the default order increment ID programmatically in your Magento 2 store.
And if you need our professional help, feel free to contact us at any time.