Magento 2 – Adjust number range

Like in Magento 1 (see here) we want in Magento too 2 adapt the number ranges. The numbers assigned by default [Storeviews number]000000000 are not really beautiful and in most cases, they are also completely disproportionate – who almost expects it 1 billion. To have to assign order numbers? So we want the order numbers, Invoice numbers, Adjust delivery note numbers and credit note numbers. A prefix should be assigned for easy assignment to the respective online shop, so that you know which shop it is about directly from a bank transfer stating the order number or support. When it comes to the number of positions, however, we will not differentiate by shop and always 6 Provide (after the prefix) have.

So we had two requirements:
1. Reduce the number of digits in the numbers to 6 Provide
2. Prefix for unique assignment to a shop project

Was in Magento 1 quickly and easily using a single table in the database was done, must be in Magento 2 (who is surprised?) can be done with significantly more effort.

The table in the database is used for the prefix “sales_sequence_profile” open. In the column “prefix” the desired prefix can then be entered – so the signs, which are placed in front of each number. These are ADDITIONAL to the 9 (or. after change 6) Place the actual number.

As you can see, are always blocks of four (1-4/5-8/…) responsible for a storeview. The first line is the order number, the second the invoice number, the third the credit note number and the fourth the delivery note number. This is repeated for each storeview. But you can also do this in the table “sales_sequence_meta” read:

This was the quick and comparatively easy part of it. So after you entered the prefix (Of course, this also works for the suffix in the column “suffix”), From this point on, all adjusted number ranges will be provided with this prefix.

Part 2 “Adjustment of the number range length” is a bit more complicated. There is a quick-and-dirty solution, which I would not recommend and a decent one.

With both solutions, the standard number length provided by Magento must be 9 Make from file “/vendor/magento/module-sales-sequence/Model/Sequence.php” (as. Line 22) overwritten.

I wouldn't even do this in a QnD solution directly in the file. Therefore the QnD approach would be as follows:

Open the file /app/etc/di.xml
There is the following code in front of the closing </config> inserted

<type name="Magento\SalesSequence\Model\Sequence">
    <arguments>
        <argument name="pattern" xsi:type="string"><![CDATA[%s%'.06d%s]]></argument>
    </arguments>
</type>

Depending on the shop, you may still have to upgrade / deployed / compiled or just cleared the cache. Then all NEW assigned numbers are only 6 digits long.

However, the disadvantage of this QnD solution is, that the change may be. could be lost in the future with an update or an adapted di.xml. So it makes more sense to write a small module, which ensures this change even after an update.

We do this as follows:

  • Create a directory path for the module such as. “app/code/Commercers/Idlength”
  • Create a file “registration.php” in this directory. This has the following content
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Commercers_Idlength',
    __DIR__
);
  • Creation of a directory “app/code/Commercers/Idlength/etc”
  • Create a file di.xml with the following content
<?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\SalesSequence\Model\Sequence">
    <arguments>
        <argument name="pattern" xsi:type="string"><![CDATA[%s%'.06d%s]]></argument>
    </arguments>
</type>
</config>
  • Create another file in this folder named “module.xml” with the following content
<?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="Commercers_Idlength" setup_version="1.0.0">
    </module>
</config>

Then you have to upgrade as above, depending on the shop / deployed / compiled or just cleared the cache. Then the numbers only have 6 Provide. If you want more or less jobs, you just have to enter the position in the di.xml you just created “06%d” adapt.

Published by Covos

Since 2009 I have been working intensively with Magento. I started with the creation and operation of B2C stores. This was extended through my work in the logistics sector. This resulted first specialized B2E systems. Today I work day-a day with exciting B2C, B2B- and B2E projects and reports in this blog about challenges and give insider tips.

Leave a Reply

Your email address will not be published. Required fields are marked *