1

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 1

3

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.

4
  • Thanks, I do what you told but nothing will happen, I was observe event customer_login and place die; on method but nothing will happen, I placed <events> in <frontend>
    – Farnabaz
    Commented Mar 10, 2014 at 22:03
  • I found where I missed up, thank you very much, but what event should observe for order completion?
    – Farnabaz
    Commented 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.
    – philwinkle
    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
    – Farnabaz
    Commented Mar 10, 2014 at 23:06

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.