Take the 2-minute tour ×
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
    
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 Mar 10 at 22:03
    
I found where I missed up, thank you very much, but what event should observe for order completion? –  Farnabaz Mar 10 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 Mar 10 at 23:00
    
thank you very much, can you please place event name in your answer, for other who has same question @philwinkle –  Farnabaz Mar 10 at 23:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.