Get Product ID Before Save Event in Magento Observer

event-observer

how can I get product id from new product before save event.I have used below code but it will create blank product in database every time.

$newProduct = $product->duplicate();
echo $product_id = $newProduct->getId();

any other way to get product id before product save.

Best Answer

What you could do is load the next value in the product table's auto_increment field. This should most of the time give you the id that will belong to the next product created.

$product = Mage::getModel('catalog/product');
$product_entity_table = $product->getResource()->getEntityTable();    
$resource = Mage::getSingleton('core/resource');
$connection = $resource->getConnection('core_read');
$result = $connection->showTableStatus($product_entity_table);
$next_product_id = $result['Auto_increment']);