Can anyone explain the difference between these events. Just the quick and dirty please. Thank you.
I have an Observer method like so:
public function detectProductChanges($observer)
{
$product = $observer->getProduct();
$old = $product->getOrigData();
$new = $product->getData();
if ($product->hasDataChanges() && $old['status'] == 1 && $new['status'] == 2) {
$this->_sendStatusMail($product);
}
}
It's not getting to the sendStatusMail()
I'm hooking into the event:
<events>
<catalog_product_save_after>
<observers>
<productchange>
<type>singleton</type>
<class>A_ProductNotification_Model_Observer</class>
<method>detectProductChanges</method>
</productchange>
</observers>
</catalog_product_save_after>
</events>
Should i be using: catalog_product_save_commit_after
GOAL:
Have an email sent after product is disabled.
private function _sendStatusMail($product)
{
if (!Mage::getStoreConfig('trans_email/ident_custom3/email')) return false;
$emailTemplate = Mage::getModel('core/email_template');
$emailTemplate->loadDefault('elec_productnotification_tpl');
$emailTemplate->setTemplateSubject('Product has been disabled');
$emailTemplate->setSenderEmail($salesData['email']);
$emailTemplateVariables['style_number'] = $product->getElecStyle();
$emailTemplateVariables['frame_color'] = $product->getAttributeText('frame_color');
$emailTemplateVariables['size'] = $product->getAttributeText('size');
$emailTemplateVariables['elec_color'] = $product->getAttributeText('elec_color');
$emailTemplateVariables['store_name'] = Mage::getModel('core/store')->load($product->getStoreId())->getName();
$emailTemplateVariables['product_name'] = Mage::getModel('catalog/product')->load($product->getId())->getName();
$emailTemplateVariables['product_sku'] = $product->getSku();
$emailTemplateVariables['dates'] = date("F jS Y h:i:sA", strtotime('-7 hours'));
// Get General email address (Admin->Configuration->General->Store Email Addresses)
$emails = explode(',', Mage::getStoreConfig('trans_email/ident_custom3/email'));
foreach ($emails as $email) $emailTemplate->send($email, $product->getStoreId(), $emailTemplateVariables);
}
}
Best Answer
Saving happens in a MySQL transaction and the
save_after
event is triggered before the transaction is committed, so that you can do additional updates in the database within the same transaction.The
save_commit_after
event is triggered after the transaction has been committed, i.e. when the changes were written to the database.Also, on
save_commit_after
, the_hasDataChanges
property already has been reset tofalse
, so your check would not work. On the other hand, if there were no changes, both events would not even be triggered, because Mage_Core_Model_Abstract::save() does nothing if there were no data changes:That being said, I don't see why your code should not work.