how to make magento generate order number by year and month and increment number for example if we in August 2017 so the order numbers to be 1708001 , 1708002 , 1708003 , 1708004 , 1708005 , 1708006 , 1708007 and so on thanks


2 Answers 2


I'm not sure if this would cause issues (the first of which if you have more than 1000 orders in a month also could cause duplicates).

The way i would probably start going about it however would be to create a custom cron job that sets the increment ID based on the current date etc and runs something like below at the start of each month:


namespace Harri\OrderUpdate\Cron;

class Updater {

    protected  _resource;

    public function __construct(Context $context,
         \Magento\Framework\App\ResourceConnection $resource)
        $this->_resource = $resource;                  

    public function execute() {
        $connection = $this->_resource->getConnection(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION);

        $ordernumber = date("y") . date("m") . "001";

        $sql = 'ALTER TABLE sequence_order_1 AUTO_INCREMENT=' . $ordernumber;


The above file 'Harri/OrderUpdate/Cron/Updater.php' is very rough but should build your order id based on the date and update the database so the next order starts with this ID.

Cron Tab:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
<group id="default"> <!-- Doesn't need to be unique -->
    <job name="order_updater" instance="Harri\OrderUpdate\Cron\Updater" method="execute">
        <schedule>0 0 1 * *</schedule>

Above file 'Harri/OrderUpdate/etc/crontab.xml' is configured to run the file above at the start of each month.

See here about cron with Magento. http://devdocs.magento.com/guides/v2.0/config-guide/cron/custom-cron-ref.html

  • default order increment id length is 9 chars, so that would allow for 99999 orders per month with id YYMM00001
    – paj
    Feb 13, 2020 at 20:48

Some merchants want to customize order numbers or invoice numbers to be different than what Magento 2 produces by default. They might want the numbers to contain more information, or they might have an existing format that shouldn’t be changed. Perhaps the numbers need to pick up where they left off from a previous website (or maybe they just don’t look pretty enough). These numbers each have an Increment ID, and the values used to create them are stored in the database and are not configurable from admin, so we’ll need a little SQL to make our customizations. Without further ado…

We can individually adjust the following properties of increment IDs for Orders, Invoices, Credit memos, & Shipments:






The Prefix, Suffix, Start-value, and Step are stored in the database, while the Pad-length is set in the code. Before changing them, let’s see how they’re all used to generate increments IDs:


In Magento 2, the method for determining the increment ID is in Magento\SalesSequence\Model\Sequence.

The pattern is set as: a string, plus a 9-digit number (padded with 0s), plus another string.

The getCurrentValue() method returns a new increment ID according to the pattern as: The Prefix, plus the return value of the calculateCurrentValue() method, plus the Suffix. The calculateCurrentValue() returns the main number:

return ($this->lastIncrementId - $this->meta->getActiveProfile()->getStartValue()) * $this->meta->getActiveProfile()->getStep() + $this->meta->getActiveProfile()->getStartValue();

For more details,pleas visit this URL :- https://www.classyllama.com/blog/m2-incrementid

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.