1

I want to know if there is a predefined list of events like this one https://www.nicksays.co.uk/magento-events-cheat-sheet-1-7/ or not because most of the events are dynamically named ?

2 Answers 2

2

I think that every Object extended from Mage_Core_Model_Abstract dispatches a lot events around loading, saving and deleting.

app/code/core/Mage/Core/Model/Abstract.php:255
Mage::dispatchEvent($this->_eventPrefix.'_load_before', $params);
// e.g. sales_order_load_before, checkout_cart_load_before

add checks, after the object was loaded

app/code/core/Mage/Core/Model/Abstract.php:267
Mage::dispatchEvent($this->_eventPrefix.'_load_after', $this->_getEventData());
// e.g. cms_page_load_after

to add additional data to the object before it is saved

app/code/core/Mage/Core/Model/Abstract.php:391
Mage::dispatchEvent($this->_eventPrefix.'_save_before', $this->_getEventData());
// e.g. catalog_product_save_before

To save other models after the "parent" was saved

app/code/core/Mage/Core/Model/Abstract.php:466  
Mage::dispatchEvent($this->_eventPrefix.'_save_after', $this->_getEventData());
// e.g. catalogrule_rule_save_after

clean up, before the model is deleted

app/code/core/Mage/Core/Model/Abstract.php:501
Mage::dispatchEvent($this->_eventPrefix.'_delete_before', $this->_getEventData());
// e.g. store_delete_before
1

Interessting question ... just to add to your answer ... there are a lot more "predefined" events:

Others from Mage_Core_Model_Abstract

  • $this->_eventPrefix.'_delete_after'
  • $this->_eventPrefix.'_delete_commit_after'
  • $this->_eventPrefix.'_clear'

For DB collections from Mage_Core_Model_Resource_Db_Collection_Abstract

  • $this->_eventPrefix.'_load_before
  • $this->_eventPrefix.'_load_after

Good idea, but for module development it's very limited since a lot of Magento model classes have no _event_prefix set. Good example for missing prefixes is class cms_block where you have to use

$object = $observer->getObject();
if ($object instanceof Mage_Cms_Model_Block) {

This just covers the model classes ...for blocks and EAV collections there are no "eventPrefixes", just some very generic events like ...

  • adminhtml_block_html_before
  • eav_collection_abstract_load_before

If your are going to write custom extenions and want to take advantage of $this->_eventPrefix add one line to your model classes:

protected $_eventPrefix = 'some_model';

So you can observe events only for this model like some_model_save_before.


Note:

_save_after is a bit missleading, it is not after saving an object, it more the last data that should be saved.

/**
 * Save object data
 *
 * @return Mage_Core_Model_Abstract
 */
public function save()
{
    ...
    try {
        $this->_beforeSave();
        if ($this->_dataSaveAllowed) {
            $this->_getResource()->save($this);
            $this->_afterSave();
        }
        $this->_getResource()->addCommitCallback(array($this, 'afterCommitCallback'))
            ->commit();
        ...
    }
}

If you want to access the saved data (i.e. the new autoincrement ID) you should use _save_commit_after.

/**
 * Callback function which called after transaction commit in resource model
 *
 * @return Mage_Core_Model_Abstract
 */
public function afterCommitCallback()
{
    Mage::dispatchEvent('model_save_commit_after', array('object'=>$this));
    Mage::dispatchEvent($this->_eventPrefix.'_save_commit_after', $this->_getEventData());
    return $this;
}
1
  • Yes good one @sv3n, thanks for these pretty additional informations
    – PЯINCƎ
    Commented Aug 8, 2017 at 22:23

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.