Chapters Close

How to change PAD length in order increment number in Magento 2?

PAD length refers to the minimum number of additional zeros assigned to each order number as “padding” until the order number becomes higher than the chosen increment pad length. For example:

  • Pad 3 = 001, 012, 123, 1234, 12345, 123456
  • Pad 4 = 0001, 0012, 0123, 1234, 12345, 123456
  • Pad 6 = 000001, 000012, 000123, 001234, 012345, 123456

In the above order increment ID consist of following parts:

  1. Prefix (store view id)
  2. Pad-length
  3. Suffix (Actual Increment id)

In Magento\SalesSequence\Model\Sequence the getCurrentValue() method set the pattern of the increment id number.

/**
* Retrieve current value
*
* @return string
*/
public function getCurrentValue()
{
   if (!isset($this->lastIncrementId)) {
       return null;
   }

   return sprintf(
       $this->pattern,
       $this->meta->getActiveProfile()->getPrefix(),
       $this->calculateCurrentValue(),
       $this->meta->getActiveProfile()->getSuffix()
   );
}

$this->pattern initially get value %s%’.09d%s from the constant DEFAULT_PATTERN.

/**
* Default pattern for Sequence
*/
const DEFAULT_PATTERN  = "%s%'.09d%s";

%’.09d sets 0 as the padding character and sets the number of digits to display as the value that follows, 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.

If you want to customize order’s Increment id PAD-length to be different than Magento 2 produces by default.

You can configure the class constructor arguments in your etc/di.xml in the argument node. The object manager injects these arguments into the class during creation. The name of the argument configured in the XML file must correspond to the name of the parameter in the constructor in the configured class.

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

The following example creates instances of Magento\SalesSequence\Model\Sequence with the class constructor argument $pattern set to a value %s%’.05d%s

Finally, run php bin/magento setup:di:compile command and test by placing a new order, increment id will be changed from 9 to 5 digits.

Speak your Mind

Post a Comment

Got a question? Have a feedback? Please feel free to leave your ideas, opinions, and questions in the comments section of our post! ❤️

* This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Thank you for your feedback

Comments (1)

  1. it changes pad length at both invoice and order number! what if i need different pad length in both?

    Reply

Grow your online business like 4,713 subscribers

    * This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
    envelope

    Thank You!

    We are reviewing your submission, and will be in touch shortly.