Back to Top

How to change Magento 2 Order’s Increment Id PAD-LENGTH ?

Updated 23 February 2024

Some merchants want to customize order’s Increment id PAD-LENGTH numbers to be different than what Magento 2 produces by default.
PAD-LENGTH is the number of zeros in increment id of an order

Increment id is the ID through which a store owner communicate with the customer.

An order increment id is consist of following parts:

  1. Prefix
  2. Suffix
  3. Start-value
  4. Pad-length

The Prefix, Suffix, Start-value, and Step are stored in the database, while the Pad-length is set in the code.

Here we only talk about PAD-LENGTH. The PAD-LENGTH of the increment-ID is determined in the code base, and it’s not affected by any database properties.
In \Magento\SalesSequence\Model\Secequence, the getCurrentValue() method used for to set the pattern of the increment id number:

Searching for an experienced
Magento 2 Company ?
Find out More

getCurrentValue()

the “$this->pattern” got value from the constant DEFAULT_PATTERN
which is initially %s%’.09d%s.

pattern

%’.09d. The ‘.0 sets “0” as the padding character and sets the number of digits to display as the value that follows, which in this case is 9. The d presents the number as a [signed] decimal. This means that by default, the increment-ID number will be a signed decimal with 9 digits, padded with 0s.

How to change PAD-LENGTH

We can change this in a custom module by creating etc/di.xml

<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">%s%'.06d%s</argument>
        </arguments>
    </type>
</config>

Here I have set the padding 6 from default padding  9 zero.

. . .

Leave a Comment

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


2 comments

  • Momchil
    • abhishek (Moderator)
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home