Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 1 of 8
Event dispatch requests
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

Hello,

Magento allows many different ways of modules’ customization, overriding methods and using events being the recommended ones.

While overriding methods is the most flexible way, it is sometimes more convenient to use Event Observers that perform actions on Event Dispatches in key spots.

An example:

Event Dispatch spot (in core module’s class):

Mage::dispatchEvent('some_event_handle', array('customer'=>$customer'order'=>$order));

Event Observer configuration (in custom module’s config.xml):

<config>
<!-- ... -->
  <
frontend>
    <
events>
      <
some_event_handle>
        <
observers>
          <
my_custom_observer>
            <
type>singleton</type>
            <class>
my_module/observer</class>
            <
method>some_event_handle</method>
          </
my_custom_observer>
        </
observers>
      </
some_event_handle>
    </
events>
  </
frontend>
<!-- ... -->
</
config>

Event Observer implementation (in custom module’s models):

class Mage_MyModule_Model_Observer
{
  
public function some_event_handle($observer)
  
{
    $event 
$observer->getEvent();
    
$order $event->getOrder();
    
$customer $event->getCustomer();
    
// ... add my custom code to be spawned during the event
    // ... may update the argument objects, or do any other action.
  
}
}

As you can see, events are a powerful mechanism providing plug and play interface for your customizations.

This thread is meant for developers, which feel that core modules missing event dispatches in key spots, would benefit majority of Magento developers and should be included with default distribution.

Please let us know what you think!

Thanks.

 
Magento Community Magento Community
Magento Community
Magento Community
 
mzentrale1
Guru
 
Total Posts:  731
Joined:  2007-12-06
Stuttgart, Germany
 

Hi Moshe,

thanks for that snippet, its a good idea i think. But for me right now its hard to understand architecture and working of magento… I tried your NewPayment Tutorial but it doesnt work, see my topic...! The last week i used much time to understand architecture and working of magento. Ill report if i had sucess creating my own paygate for cc.

KR

Stefan

 
Magento Community Magento Community
Magento Community
Magento Community
 
Mark_Kimsal
Sr. Member
 
Total Posts:  186
Joined:  2007-09-12
Michigan, USA
 

Need an event when:

* a quote_item gets turned into an order_item (I think this one is done)
* a catalog_product gets turned into a quote_item
* before and _before_ a customer logs-in and logs-out (great for doing CMS integration with a just a helper)
* before and after a customer changes some profile data (ditto)

 
Magento Community Magento Community
Magento Community
Magento Community
 
Mark_Kimsal
Sr. Member
 
Total Posts:  186
Joined:  2007-09-12
Michigan, USA
 

I’m also thinking of:

* When someone views an item (might give ability to track some stats separately, or double check inventory)
* before and after someone removes an item from the cart
* before and after someone changes quantity in the cart
* before and after any emails are sent to any customer
* before and after any payment gateways are created and/or used

The cart quantity and delete ones I was thinking about making some offers, like buy one get one free.  Well, you don’t want them buying one, and then deleting the one they have to pay for.  Or, buying one and then changing the quantity of the free one… so listening to those events would be helpful.

Before and after emails would allow someone to check another list of do-not-mail or another subscription list before sending.  After sending they can log the mail to a special database or something without having to override the entire method.

Payment gateways… just seems like someone would want to be notified of that stuff.  Perhaps to unencrypt a key, or to trigger an email if an order is over X amount of dollars (maybe to watch it for fraud).

 
Magento Community Magento Community
Magento Community
Magento Community
 
Mark_Kimsal
Sr. Member
 
Total Posts:  186
Joined:  2007-09-12
Michigan, USA
 

hopefully the last one:

* Right before a new registration is saved

Sometimes you want to add 1 or 2 flags to a user, again the event is much easier than over ridding a complete class/method.

 
Magento Community Magento Community
Magento Community
Magento Community
 
bigodines
Jr. Member
 
Avatar
Total Posts:  27
Joined:  2008-01-05
Brazil
 

* Right before a new registration is saved

I second this as it will help a lot while integrating magento with other systems.

before/after user regsitration/profile changes/user removal is a _must_

I’m not sure which events are already implemented. But I would like to add to the list:

- before/after page loads
- before/after logout would be nice as well.
- before search could be useful to trigger external search mechanisms (services?)

events during cart submission/modification as Mark suggested are a really good idea.

just my 0.02 smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
srinigenie
Guru
 
Avatar
Total Posts:  539
Joined:  2008-02-04
 

Thought a wiki would best describe usage of this functionality. Didn’t have the time to fully document the code and list of events. I have extracted a list of events that magento currently supports in its code. Please have a look at
Customization using Event-Observer Wiki

 
Magento Community Magento Community
Magento Community
Magento Community
 
bigodines
Jr. Member
 
Avatar
Total Posts:  27
Joined:  2008-01-05
Brazil
 
srinigenie - 04 March 2008 08:24 AM

Thought a wiki would best describe usage of this functionality. Didn’t have the time to fully document the code and list of events. I have extracted a list of events that magento currently supports in its code. Please have a look at
Customization using Event-Observer Wiki

thanks for providing the list. Now we just need to find out when these events are called rasberry

 
Magento Community Magento Community
Magento Community
Magento Community
 
srinigenie
Guru
 
Avatar
Total Posts:  539
Joined:  2008-02-04
 

Will these events be raised even during product import process? say, will ‘catalog_product_save_after’ be raised after importing the product using bulk import functionality?

 
Magento Community Magento Community
Magento Community
Magento Community
 
peterw83
Member
 
Total Posts:  44
Joined:  2007-11-12
 

Will we have more documentation about events after 1.0. ?

A Wiki entry has been created but not contributions yet. 
http://www.magentocommerce.com/wiki/customizing_magento_using_event-observer_method?s=events

 
Magento Community Magento Community
Magento Community
Magento Community
 
Mark_Kimsal
Sr. Member
 
Total Posts:  186
Joined:  2007-09-12
Michigan, USA
 

I need an event every time a “transactional email” is sent.  At least the new order and order update e-mails.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Bojan Hrnkas
Member
 
Total Posts:  51
Joined:  2008-04-15
 

Hi! How do I create my own event?

I want to do something similar to cron.php, but I don’t know how to initialise Mage class before calling Mage::dispatchEvent(’my_event’);

Thanks!

 
Magento Community Magento Community
Magento Community
Magento Community
 
srinigenie
Guru
 
Avatar
Total Posts:  539
Joined:  2008-02-04
 

umm ..finally got some time to update the wiki on event-observer with a full fledged example...have a look and let me know your feedback

 
Magento Community Magento Community
Magento Community
Magento Community
 
mzentrale1
Guru
 
Total Posts:  731
Joined:  2007-12-06
Stuttgart, Germany
 

Hello Moshe,

within Onepage chekcout it would be great to have some more observers for saving additional customer/address data. This could be very helpful to implement german and european needs at registration of customers. i had to overload saveorder to save additonal datas if user isnt registrated.

cheers

stefan

 
Magento Community Magento Community
Magento Community
Magento Community
 
winans_fred
Sr. Member
 
Total Posts:  83
Joined:  2007-09-06
 

Personally, I think it would be great to go maximum flexibility and expose event dispatch in a way that we could specify the method in which we need the event to occur, and then after what call we want it to occur, since an event always happens after some method is called, it would make it so much easier if the core provided the inherent functionality to register our own event dispatches only by requiring certain information which we would simply find and refer to from the source code. 

All that being said, I need an event dispatch for the CartController in the Checkout module - specifically for deleteAction() for a module I’m working on.

Oh, and by the way.  You have no idea how happy I am that you’ve integrated the event dispatch system.  NO IDEA!!!

Regards,

Fred

 
Magento Community Magento Community
Magento Community
Magento Community
 
smckeon
Jr. Member
 
Total Posts:  1
Joined:  2008-05-20
 
winans_fred - 19 May 2008 06:37 AM

Personally, I think it would be great to go maximum flexibility and expose event dispatch in a way that we could specify the method in which we need the event to occur, and then after what call we want it to occur, since an event always happens after some method is called, it would make it so much easier if the core provided the inherent functionality to register our own event dispatches only by requiring certain information which we would simply find and refer to from the source code.

That would dangerously couple your event handler to the internal workings of the core - sure it would give you more control but it’d be fragile, breaking if so much as a method name was changed.  If you’re going to be that tightly coupled, you might as well just modify the class directly.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 8