1

I have created an observer for auto-generating SKU when admin add product.

This is my Custom_Module/Model/Observer.php

class Bookandgo_AutoSku_Model_Observer
{
    public function autoGenerateSku($observer)
    {
        $product = $observer->getEvent()->getProduct();
        $sku = "TA1000";
        $product->setSku($sku);
    }
}

It successfully generate SKU, now how i make this SKU auto increment? Also i want to make SKU based on selected attribute set. For example if i select Default attribute, it will be DF1000, but if i select Tour and Adventure attribute, it will be TA1000.

2 Answers 2

1

You can use this:

public function catalogProductNewAction(Varien_Event_Observer $observer)
{
    $product = $observer->getProduct();

    $products = Mage::getResourceModel('catalog/product_collection')
        ->addAttributeToFilter('attribute_set_id', $product->getAttributeSetId())
        ->addAttributeToSort('entity_id', Zend_Db_Select::SQL_DESC)
        ->setPage(1, 1);

    $sku = filter_var($products->getLastItem()->getSku(), FILTER_SANITIZE_NUMBER_INT);
    if ($sku) {
        $sku = $sku + 1;
    } else {
        $sku = 1000;
    }

    $prefix = 'DF';
    if ($product->getAttributeSetId() != 4) {
        $prefix = 'XYZ';
    }

    $product->setSku($prefix . $sku);
}

Note: adjust attribute set IDs and prefixes to your needs:

$prefix = 'DF';
if ($product->getAttributeSetId() != 4) {
    $prefix = 'XYZ';
}
2
  • I have a problem, at first it generate well but then when i insert second record i got SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '37-1' for key 'UNQ_CATALOGINVENTORY_STOCK_ITEM_PRODUCT_ID_STOCK_ID any solution for this?
    – Seventh St
    Jun 14, 2017 at 1:19
  • Which event do you use? Can you please check the SKU that are generated? (tested it and i could create multible products in a row)
    – sv3n
    Jun 14, 2017 at 10:22
1

Try this

<?php
class Bookandgo_AutoSku_Model_Observer {
  public function autoGenerateSku($observer) {
    $productColl = Mage::getResourceModel('catalog/product_collection');
    $productColl->addAttributeToSort('entity_id', 'DESC');
    $productColl->setPage(1,1);
    $lastproduct=$productColl->getLastItem()
    $lastproductAttributeSetName= Mage::getModel('eav/entity_attribute_set')->load($lastproduct->getAttributeSetId())->getAttributeSetName();
    $lastSku='';
    if($lastproductAttributeSetName=="Default"){
      $lastSku = ltrim($lastproduct->getSku(),"DA");
     }
    elseif($lastproductAttributeSetName=="Tour and Adventure"){
      $lastSku = ltrim($lastproduct->getSku(),"DA");
    }

    $newSku=$lastSku+1;

    $product = $observer->getEvent()->getProduct();
    $newproductAttributeSetName= Mage::getModel('eav/entity_attribute_set')->load($product ->getAttributeSetId())->getAttributeSetName();
   if($lastproductAttributeSetName=="Default"){
      $product->setSku('DA'.$newSku);
     }
    elseif($lastproductAttributeSetName=="Tour and Adventure"){
       $product->setSku('TA'.$newSku);
    }

  }
}
2
  • i know this is a sudden, but please see my edited question
    – Seventh St
    Jun 12, 2017 at 10:26
  • Check my edited answer. Jun 12, 2017 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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