I am trying to create a code that generated:

1) a random prefix sku with four digits(3 integers and 1 char) that depends on the category (on the last sub category) so for all product that are in the same sub-category the prefix should be the same!

2) a random suffix three digits (it could be only integers or 2 integers 1 char but no only char's)

3) between the prefix and suffix the should be a - (minus)

I already have a code that creates a the next sku, that Increment the SKU of the last edit product

$dbread = Mage::getSingleton('core/resource')->getConnection('core_read');
$sql = $dbread->query("SELECT * FROM catalog_product_entity ORDER BY created_at DESC LIMIT 1");
$res = $sql->fetch();
 <script type="text/javascript">
   if(document.getElementById('sku').value == ""){
 document.getElementById('sku').value = <?php echo (int)$res["sku"] + 1; ?>;

Hope you could help me


SO that is the category and if I add a product and Select X more then one category, then it should take only the prefix from the last selected category, the last one is Men

    Clothing & Shoes   x                      Prefix: 2088-
                    Clothing    x             Prefix: 2058-
                            Men    x          Prefix: 2002-

maybe we could take th category id number + 2 digits more if there is only one or two digits only

  • What should happen when the category is not selected or if the category is changed? Have you modified the application to prevent these scenarios?
    – beeplogic
    Commented Jun 8, 2014 at 15:05
  • @beeplogic if the category is no category is selected then good question:) hmm... I think then it should give the product a special prefix maxe five five digits and if the category will be change then it should take the last category prefix I have update my question so I you can see want I am trying.... but I now Ithat i have to modify the database and also modify the code above not sure how
    – Kate
    Commented Jun 8, 2014 at 17:39
  • It is not recommended that you use raw sql for getting products info.
    – Eli Y
    Commented Jul 7, 2014 at 6:04
  • Inclined to close as this scenario seems unlikely to help others and impossibly broad given Magento's assumptions regarding SKUs: they are assigned when products are created (hence pre-category-association) and URL keys are generated therefrom.
    – benmarks
    Commented Feb 15, 2015 at 16:48

1 Answer 1


A simple adminhtml observer configured for catalog_product_save_before should allow you to generate a SKU per your requirements once category assignments have been made. You may want to set a static attribute flag for when a product has had this done, and you may also want to enforce that a product only has one category. This (and even SKU stuff) can be done by observing catalog_product_prepare_save.

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