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
<?php
$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; ?>;
}
</script>
Hope you could help me
EDIT
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
Example
root
Clothing & Shoes x Prefix: 2088-
Clothing x Prefix: 2058-
Women
Men x Prefix: 2002-
Shoes
maybe we could take th category id number + 2 digits more if there is only one or two digits only
Best Answer
A simple
adminhtml
observer configured forcatalog_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 observingcatalog_product_prepare_save
.