I'm new to magento and i don't have any idea about how I can run specific PHP function when user order completion.
can anyone explain me how I can do this?
I read about event/observer
but I don't understand how to use
Thanks
1 Answer
An "observer" is a fancy name for a method on a class definition that is executed on an event. Any number of observers can be fired for any one event.
To create one you first create a module. In the module config.xml
definition locate (or create) the <global>
, <frontend>
and <adminhtml>
nodes. You can create the event listener in any one of those 3 nodes. To register it, do the following:
<events>
<event_name_here>
<observers>
<store>
<class>yourmodule/observer</class>
<method>methodName</method>
</store>
</observers>
</event_name_here>
</events>
To get a list of valid event names you can visit this site here.
In your module, create a class file called Observer.php
in the Model
directory. It will look something like:
class YourCompany_YourModule_Model_Observer
{
public function methodName($observer)
{
//observer data is in $observer->getEvent()
}
}
From there, whenever the event name you're listening to is called, your module's Observer model will be loaded and the method you've registered will fire.
-
Thanks, I do what you told but nothing will happen, I was observe event
customer_login
and placedie;
on method but nothing will happen, I placed<events>
in<frontend>
– FarnabazCommented Mar 10, 2014 at 22:03 -
I found where I missed up, thank you very much, but what event should observe for order completion?– FarnabazCommented Mar 10, 2014 at 22:12
-
sales_order_place_after
is always a good bet - but be careful; exceptions raised in this event may prevent order entry. Commented Mar 10, 2014 at 23:00 -
thank you very much, can you please place event name in your answer, for other who has same question @philwinkle– FarnabazCommented Mar 10, 2014 at 23:06