Magento – Magento2 Auto Increment SKU

magento2sku

I am willing to get product SKU value to auto-increment in Magento 2. For example the last product created sku was 100001 then the next product would be 100002 and so on.

Best Answer

Till now Magento does not have such functionality. Though Magento provides the feature to set the mask for autogenerating SKU field.

To do so, go to

Stores > Configuration > Catalog > Product Fields Auto-Generation

and set value of Mask for SKU field.

If you still want to auto-generate SKU field like 100001, 100002 etc., then you need to create a custom module which will observe the event of product edit form load. On this event, check whether it is an old product being edited or a new product is being created.

If a new product is being created, then you would need to get last SKU from the table and fill the value of SKU with (last SKU + 1) value.

Note: Before doing above exercise, you would need to make two changes for SKU attribute:

  • Change SKU data type to integer
  • Make the SKU field read-only, so that admin cannot change the generated value

Please check and let me know if you have any question.

Related Topic