3

What is the difference between catalog_product_prepare_save and catalog_product_save_before?

I have an observer that updates some attributes when a product is saved. It works great when I save a product from admin, however it doesn't work when saving from a script I wrote.

Through my research in writing the observer, I was using catalog_product_save_before. Someone on here told me I should be using catalog_product_prepare_save. Although it didn't solve the problem at the time, I kept with it as eventually I got everything working.

My new research is telling me when I call $product->save(), it doesn't trigger catalog_product_prepare_save, but it does trigger catalog_product_save_before, so I'm considering switching my observer to catalog_product_save_before but can't find much documentation between the two. Wondering if I'm going to experience any adverse behavior by switching since I'm not sure why several people told me to use catalog_product_prepare_save in the first place.

5

catalog_product_save_before is one of standard events called by the Mage_Core_Model_Abstract class in its _beforeSave() method. The line looks like this:

Mage::dispatchEvent($this->_eventPrefix.'_save_before', $this->_getEventData());

For every model that extends Mage_Core_Model_Abstract a set of standard events are run which other modules may hook into without the need to write them manually in your class. They are, among others:

  • {$prefix}_save_before
  • {$prefix}_save_after
  • {$prefix}_delete_before
  • {$prefix}_delete_after
  • ${prefix}_save_commit_after
  • ${prefix}_delete_commit_after

.. etc. Prefix is taken from protected property _eventPrefix which by default is set to core_abstract. You should only set that in any of your classes to make those standard events specific to your model.

On the other hand catalog_product_prepare_save is a custom event called only in 2 places in magento: Mage_Catalog_Model_Product_Option_Api and Mage_Adminhtml_Catalog_ProductController so you can use them to hook into product update done via admin panel or API but not any other means.

  • Thank you. So is there any issue with using save_before instead of prepare_save? – lv2fly Jun 26 '17 at 0:40
  • No, it is not. You need to only be aware it will be used anytime any part of the code uses save() method on Mage_Catalog_Model_Product class. On the other hand it does not guarantee you that you will catch all instances when product data are changed. Some module may directly call $product->getResource()->save($product) which will skip those default events from model layer. And there is function saveAttribute() in Mage_Eav_Model_Entity_Abstract (and Mage_Catalog_Model_Resource_Product extends it) which directly updates attribute related table. – Zefiryn Jun 26 '17 at 6:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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