2

I need to manipulate invoice items & some other data after the invoice is created, so I've used sales_order_invoice_save_after event.

I know this event is fired more than just when creating the invoice, so most people recommend using other events such as sales_order_invoice_pay, but that's not a problem for my needs.

My surprise has came when debugging why things don't work as expected, and I've found that it seems this event is fired before the invoice is totally saved

For instance, inside the observer...

$invoice->getData() // returns some data, but...
$invoice->getId() // returns nothing ¿¿??

If I look database, there is no record in sales_flat_invoice table

Anybody here has faced the same issue? So, which event could I use to get the invoice object after it is saved?

2

It is fired after the save action, but before data is commited. sales_order_invoice_save_commit_after should do the job.

In Mage_Core_Model_Abstract you can see whats the difference is between _save_before, _save_after and _save_commit_after events are:

/**
 * 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();
        ...
    }
    return $this;
}

/**
 * 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;
}
  • nice! I will try & give you feedback – Raul Sanchez May 5 '17 at 17:24

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.