1

I have an observer method that triggers upon the event sales_order_creditmemo_save_after, basically when a credit memo was created. The method I have fires:

public function handleCreditmemoCreation(Varien_Event_Observer $observer)
    {
        //$observer contains data passed from when the event was triggered.
        $fp = fopen('/Applications/MAMP/htdocs/magento-1/var/log/request.log', 'a');
        fwrite($fp, $observer->getData());
        fwrite($fp, 'test');
        fclose($fp);
    }

However, $observer->getData() doesn't output anything into the request.log log file. To confirm I configured the file correctly, I write the string 'test' to the file, which works.

Also, when I check $observer for null, it tells me that $observer is not null. Does anyone know why then it does not seem to contain anything?

1 Answer 1

0

To access observer passed data you'd always use $observer->getEvent()

$observer->getEvent()->getData(XXX)

Where XXX is variable, as every event can contain different data

In your case, if you browse Mage_Sales_Model_Order_Creditmemo class, you will find this properties

protected $_eventPrefix = 'sales_order_creditmemo';
protected $_eventObject = 'creditmemo';

First of them tells us the event prefix this model will use when firing standard Magento model events. For instance: sales_order_creditmemo_save_after (yours), sales_order_creditmemo_delete_before, etc...

The second tells us the object passed in that events. So, in your concrete case, you can access creditmemo object this way...

$observer->getEvent()->getCreditmemo();
$observer->getEvent()->getData('creditmemo'); // same result
3
  • hey, thanks for the reply. I tried your code; however, I still get no data:(
    – Kevin Wu
    Commented Jan 28, 2016 at 21:38
  • You can update your question with the updated code, so I can see what are you trying to do. For instance, you cannot log a XXX->getData() as a string, cause it is not a string. Do a print_r(XXX->getData()), and then check what info do you want to log Commented Jan 28, 2016 at 21:42
  • ok, it worked. Thanks a lot. It turns out that fwrite() can only log strings as you mentioned. Using print_r, I was able to print the returned array to a log file.
    – Kevin Wu
    Commented Jan 29, 2016 at 0:02

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.