Magento – Auto Generate SKU

attributessku

I would like to auto generate SKU with 6 or 8 digits which will consist of first 2 digits for manufacturer, next 1 digit for Colour, next 1 digit for Supplier and last 2 or 4 random numbers or auto increment.

eg. I would assign Manufacturer: Sony with number 15, Colour: Red with 5, Supplier: 7

Random or Auto Increment numbers: 0028

Final SKU: 15570028 [Unique entry in database]

Any who could help with this?

Thank You

Edit: Ideally, it should check which attribute are selected [Colour, Manufacturer] and upon save, numbers assigned against those attributes value will be taken in the process of generating SKU

Edit 1: This would be while add/edit a product from admin. Ideal condition would be when all the required attributes are selected such as Colour, Manufacturer, Supplier – only then it should generate SKU and save it to database.

Best Answer

You can do this via the event 'catalog_product_save_before', but in your observer limit the run of the code to only the admin save.

Refer to this excellent answer on how to do that: Is there an event for after a product is saved only in admin?

You can then change the SKU accordingly. All the attribute data will be available to you at this point.

You can choose to put the SKU attribute as not required, as anything you enter into it will get replaced in the observer. Personally I would not, as you run the risk of having products with no sku value in the system if something in the code goes wrong.

Related Topic