3

I have an observer listening for events that fire sales_order_creditmemo_save_after. The issue I have is that when a credit memo is saved on the invoice, the sales_order_invoice_save_after event is also fired - which causes issues with my code, because I also have another observer listening on that event.

For the observer listening on sales_order_invoice_save_after - is there a way to distinguish whether the event was a credit memo or an invoice?

I have found deep down in the $observer object, protected '_historyEntityName' => string 'creditmemo' (length=10) however is there a more standard way to access this information?

2 Answers 2

0

The work around was to add a conditional check on the sales_order_invoice_save_after to look for the order status label. Because I only needed the observer to work when the order status was 'Processing', I was able to use:

if ($order->getStatusLabel() == 'Processing')

However I am yet to find a way to access protected '_historyEntityName' => string 'creditmemo' (length=10) in the observer object.

1
  • 1
    I just grepped in the Magento core for _historyEntityName. It is used very rarely and you do not have any chance to access it from the outside.
    – Simon
    Commented Feb 19, 2015 at 8:04
0

You can use Mage::register($name,$value) and Mage::registry($name) to solve this issue.

Inside your sales_order_creditmemo_save_after event add this:

$orderid = $observer->getEvent()->getCreditmemo()->getOrderId();
Mage::register('creditmemo_created_for_order_'.$orderid,true);

At the beginning of your sales_order_invoice_save_after event, add this:

$orderid = $observer->getEvent()->getInvoice()->getOrderId();
if(Mage::registry('creditmemo_created_for_order_'.$orderid)) { return; }

What this will do is store the value "true" into a temporary cache (Mage::registry) that is accessible from any script for that ONE request. Once the page has finished loading, that value is no longer stored.

Because both events are triggered in one request, it means that the first event (sales_order_creditmemo_save_after) stores the value and the second event (sales_order_invoice_save_after) checks for it's existence and if it exists it will exit out of your observer function.

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.