Module Event-Observer – Will $product->save() Trigger catalog_product_save_after?

event-observermodule

I have an module A will save a product using

$product->save();

This module will run in cron job.

And another module B has

<catalog_product_save_after>
   <observers>
      <warehouse_save_product_batch_special_prices>
        <class>warehouse/catalog_product_price_observer</class>
        <method>saveBatchSpecialPrices</method>
      </warehouse_save_product_batch_special_prices>
   </observers>
</catalog_product_save_after>

Anyone know module A will or will not trigger module B function?

Best Answer

Yes, definitely, if you check the save function of Mage_Core_Model_Abstract you can see this:

public function save()
{
    /**
     * Direct deleted items to delete method
     */
    if ($this->isDeleted()) {
        return $this->delete();
    }
    if (!$this->_hasModelChanged()) {
        return $this;
    }
    $this->_getResource()->beginTransaction();
    $dataCommited = false;
    try {
        $this->_beforeSave();
        if ($this->_dataSaveAllowed) {
            $this->_getResource()->save($this);
            $this->_afterSave();
        }
        $this->_getResource()->addCommitCallback(array($this, 'afterCommitCallback'))
            ->commit();
        $this->_hasDataChanges = false;
        $dataCommited = true;
    } catch (Exception $e) {
        $this->_getResource()->rollBack();
        $this->_hasDataChanges = true;
        throw $e;
    }
    if ($dataCommited) {
        $this->_afterSaveCommit();
    }
    return $this;
}

This line: $this->_beforeSave(); triggers the following events:

  • model_save_before
  • $this->_eventPrefix.'_save_before' where the event prefix depends on your model. In your case, it is catalog_product

On the other hand, this line: $this->_afterSave() triggers the following events:

  • model_save_after
  • $this->_eventPrefix.'_save_after' (same remark as before)