Your shopping cart is empty. Browse our Store

Magento

Open Source eCommerce Evolved

Magento Forum

   
Event dispatch requests
 
Moshe
Magento Team
 
Avatar
Total Posts:  1524
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.

 Signature 

- I would love to change the world, but they won’t give me the source code -

 
Magento Community Magento Community
Magento Community
Magento Community
 
[m] zentrale
Sr. Member
 
Avatar
Total Posts:  221
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

 Signature 

Account-User: S. Bothner & A. Jaus
[m] zentrale | InternetAgentur Stuttgart & München
Agentur für Beratung, Entwicklung & Marketing.
----
Our first Magento™ shop online now: Stella Lena N.Y.

 
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)

 Signature 

There’s info on lots of custom modules in my Magento Group
http://www.magentocommerce.com/group/view/174
Need Upgrade help from 0.6, 0.7, and 0.8 available?  P.M. me for details on CMS integration.

 
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).

 Signature 

There’s info on lots of custom modules in my Magento Group
http://www.magentocommerce.com/group/view/174
Need Upgrade help from 0.6, 0.7, and 0.8 available?  P.M. me for details on CMS integration.

 
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.

 Signature 

There’s info on lots of custom modules in my Magento Group
http://www.magentocommerce.com/group/view/174
Need Upgrade help from 0.6, 0.7, and 0.8 available?  P.M. me for details on CMS integration.

 
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

 Signature 

Matheus Mendes
Meu blog in portuguese
My blog in “english”

 
Magento Community Magento Community
Magento Community
Magento Community
 
srinigenie
Guru
 
Avatar
Total Posts:  306
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 09: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

 Signature 

Matheus Mendes
Meu blog in portuguese
My blog in “english”

 
Magento Community Magento Community
Magento Community
Magento Community
 
srinigenie
Guru
 
Avatar
Total Posts:  306
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:  42
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.

 Signature 

There’s info on lots of custom modules in my Magento Group
http://www.magentocommerce.com/group/view/174
Need Upgrade help from 0.6, 0.7, and 0.8 available?  P.M. me for details on CMS integration.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Bojan Hrnkas
Jr. Member
 
Total Posts:  9
Joined:  2008-04-16
 

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:  306
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
 
[m] zentrale
Sr. Member
 
Avatar
Total Posts:  221
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

 Signature 

Account-User: S. Bothner & A. Jaus
[m] zentrale | InternetAgentur Stuttgart & München
Agentur für Beratung, Entwicklung & Marketing.
----
Our first Magento™ shop online now: Stella Lena N.Y.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
 
© Copyright 2008 Varien. Magento is a trademark of Irubin Consulting Inc. DBA Varien