Question

I have set up a default 'no modifications' copy of magento on a local machine to replicate the results on our production machine.

When I try to catch the event 'adminhtml_customer_save_after' in a module, it always returns / stops on customer_save_before. This is also true for any of the customer_save_after type events.

Adding

file_put_contents('/tmp/events.log','Dispatching '. $name. "\n",FILE_APPEND); 

to the dispatchEvent function in /var/www/app/Mage.php verifies that it indeed does return on customer_save_before, even though that is not the event i'm asking for.

Please validate this and let me know if this is intended functionality. I must have access to the entity_id in magento admin section when our call center / order team creates new customers in the admin side of the site (for placing phone-in orders) for synchronization with our company's database.

PHP from module

<?php
class NKI_CustomerSync_Model_Observer
{

public function AddCustomerToQueue($observer)
{

$event = $observer->getEvent();
$customer = $event->getCustomer();

$model=$event->getModel();

echo "<PRE>";
var_dump($event->getName());
var_dump($event->getData());

var_dump($event);
var_dump(get_class_methods($event));
die();
}.....

XML

<config>
<modules>
<NKI_CustomerSync>
<version>0.1.0</version>
</NKI_CustomerSync>
</modules>

<global>
<events>
<adminhtml_customer_save_after>
<observers>
<NKI_customersync_model_observer>
<type>singleton</type>
<class>NKI_CustomerSync_Model_Observer</class>
<method>AddCustomerToQueue</method>
</NKI_customersync_model_observer>
</observers>
</adminhtml_customer_save_after>
Was it helpful?

Solution

First, since your post betrays a few misunderstandings about how Magento's event system works, which in turn is leading you to diagnose the problem incorrectly, here's a quick review of the event system.

The dispatchEvent method in Mage.php is the wrong place to check if Magento "catches" your event. This method receives all events. It's not until deeper in the calling chain, in Mage_Core_Model_App's identically named dispatchEvent method

#File: app/code/core/Mage/Core/Model/App.php
public function dispatchEvent($eventName, $args)
{
    foreach ($events[$eventName]['observers'] as $obsName=>$obs) {    
        //...
    }
}

where Magento will look for any event observers (or in your parlance, event catchers)

Magento issues all sorts of events on every request. The customer_save_before event is issued whenever a customer model object is saved. This includes saving both on the frontend and the backend. However, the adminhtml_customer_save_after event is fired here

#File: app/code/core/Mage/Adminhtml/controllers/CustomerController.php
public function saveAction()
{
    //...
    $customer->save();
    //...
    Mage::dispatchEvent('adminhtml_customer_prepare_save', array(
        'customer'  => $customer,
        'request'   => $this->getRequest()
    ));        
    //..
}

In other words, this event fires in the saveAction of the admin customer controller. In other other words, this event fires after a user clicks "save" in the Magento admin console when looking at an individual customer.

So, both the customer_save_before and the adminhtml_customer_save_after event will fire when a customer gets saved in the Magento admin. The customer_save_before event fires first, and then the adminhtml_customer_save_after event fires.

As for your specific code, what you've shown looks correct. That assuming you have the closing } on the observer class, and that it's in the correct location.

app/code/community/NKI/CustomerSync/Model/Observer.php

//or, if your module is configured in the local code pool

app/code/local/NKI/CustomerSync/Model/Observer.php    

and that your module's config.xml has a closing </config>, is valid XML, and is in your module's etc/config.xml file. This also assumes Magento can see your module.

I used your code to throw together a skeleton module and your event fired when I saved a customer on the backend of Magento. That skeleton module is here. Compare it to what you have to see where your module may be subtly incorrect.

OTHER TIPS

The Event Should be put in adminhtml and is called customer_save_after

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top