1

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.

4
  • Are you going to import any product csv or where you want to do this?
    – Sukeshini
    Oct 29 '14 at 7:22
  • Updated question under Edit 1
    – harry
    Oct 29 '14 at 7:34
  • I wouldn't use just numbers: simplehelix.com/blog/magento/best-practices-for-magento-skus "Don’t Use Just Numbers -When exporting with dataflow, numeric-only SKUs are in danger of being formatted incorrectly. I would recommend appending or prefixing alpha characters as a general rule if numbers have to be used."
    – B00MER
    Oct 29 '14 at 7:41
  • This above statement is incorrect. Your just as likely to format letters incorrect too. For example, c & C are not the same. Most WMS use a numerically based system, hence why UPC is a standard for most systems. So if you using barcodes at all I'd highly recommend keep it numerical. I've processed millions of SKU like this. Mar 25 '16 at 3:21
2

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.

1

If you are creating products from Admin Panel, set SKU attribute not required and update the produts with a script, which will take all the values and put it in SKU attribute.

But, if you're creating Products through API calls, you can create a new module for product creation, or can extend and modify the default catalogProductCreate api where following codes will help you:

public function create($type, $set, $sku, $productData, $store = null)
    {
        if (empty($sku)) {
            $sku = $productData['manufacturer'].$productData['color'].$productData['supplier'];
        }
        if (!$type || !$set || !$sku) {
            $this->_fault('data_invalid');
        }

        $this->_checkProductTypeExists($type);
        $this->_checkProductAttributeSet($set);

        /** @var $product Mage_Catalog_Model_Product */
        $product = Mage::getModel('catalog/product');
        $product->setStoreId($this->_getStoreId($store))
            ->setAttributeSetId($set)
            ->setTypeId($type)
                ->setSku($sku);
        ...
        ...
5
  • Currently, I am creating products from admin, so updating sku through script would suit me. Could you please explain a bit more on how can it carried out with script? Thank you
    – harry
    Oct 29 '14 at 7:53
  • Catalog -> Manage Attributes -> select attribute code 'sku'. Set Values Required to 'No'. Now create an api method where following code will be placed: `$collection = Mage::getModel('catalog/product')->getCollection(); Oct 29 '14 at 7:57
  • yes, I have already set No under value required. Further, when we update with script, SKU must be updated in more than one table in database, also we need to identify which table(s) consist attribute value for a particular product. How to achieve this?
    – harry
    Oct 29 '14 at 8:02
  • Catalog -> Manage Attributes -> select attribute code 'sku'. Set Values Required to 'No'. Now create an api method where following code will be placed: $collection = Mage::getModel('catalog/product')->getCollection(); foreach($collection as $product){ if(!empty($product->getSku())) { $sku = $product->getData('manufacturer').$product->getData('color').$product->getData('supplier'); $product->setSku($sku)->save(); } } Oct 29 '14 at 8:07
  • the code is not arranged, please re-arrange it and then proceed with it Oct 29 '14 at 8:08

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