I have a custom module which is used to observe for cusotmer_save_after
event in admin side. I have added a custom tab profile at glance
in Magento's customer module. I am using my module to store data that is collected through this custom tab in my custom table customer_customization
.
My XML file is in app/code/local/Electronicsstore/Customization/etc/config.xml
<config>
<modules>
<Electronicsstore_Customization>
<version>0.1.0</version>
</Electronicsstore_Customization>
</modules>
<frontend>
<routers>
<customization>
<use>standard</use>
<args>
<module>Electronicsstore_Customization</module>
<frontName>customization</frontName>
</args>
</customization>
</routers>
<layout>
<updates>
<customization>
<file>customization.xml</file>
</customization>
</updates>
</layout>
</frontend>
<admin>
<routers>
<customization>
<use>admin</use>
<args>
<module>Electronicsstore_Customization</module>
<frontName>customization</frontName>
</args>
</customization>
</routers>
</admin>
<adminhtml>
<layout>
<updates>
<customization>
<file>customization.xml</file>
</customization>
</updates>
</layout>
<events>
<adminhtml_customer_save_after>
<observers>
<electronicsstore_customization>
<type>model</type>
<class>electronicsstore_customization_model_observer</class>
<method>saveCustomerCustomizationData</method>
</electronicsstore_customization>
</observers>
</adminhtml_customer_save_after>
</events>
</adminhtml>
<global>
<models>
<customization>
<class>Electronicsstore_Customization_Model</class>
<resourceModel>customization_mysql4</resourceModel>
</customization>
<customization_mysql4>
<class>Electronicsstore_Customization_Model_Mysql4</class>
<entities>
<customization>
<table>customer_customization</table>
</customization>
</entities>
</customization_mysql4>
</models>
<resources>
<customization_setup>
<setup>
<module>Electronicsstore_Customization</module>
</setup>
<connection>
<use>core_setup</use>
</connection>
</customization_setup>
<customization_write>
<connection>
<use>core_write</use>
</connection>
</customization_write>
<customization_read>
<connection>
<use>core_read</use>
</connection>
</customizations_read>
</resources>
<helpers>
<customization>
<class>Electronicsstore_Customization_Helper</class>
</customization>
</helpers>
</global>
</config>
My model observer file looks like this.
app/code/local/Electronicsstore/Customization/model/Observer.php
<?php
class Electronicsstore_Customization_Model_Observer
{
/**
* This method will run when the customer is saved from the Magento Admin
* Use this function to update the customer model, process the
* data or anything you like
*
* @param Varien_Event_Observer $observer
*/
public function saveCustomerCustomizationData(Varien_Event_Observer $observer)
{
echo "i am here";die();
try {
// do some model loads and checks here so that you will update existing data and not duplicate rows on editAction saves...\
$frontendModel = Mage::getModel( 'customization/customization' );
$frontendCollection=$frontendModel->getCollection()->addFieldToFilter('parent_id', array('eq' => $object->getCustomerId()))->getFirstItem();
if(!$frontendCollection->isEmpty())
{
$frontendCollection->addData($object->getData())->save();
}
else
{
//$frontendModel->addData('customization_parent_id',$object->getId())->save();
$frontendModel->setData($object->getData());
$frontendModel->setData("parent_id",$object->getId());
$frontendModel->save();
}
}
catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
}
}
}
}
Note: die() is not working while saving customer data.
However, my observer seems to be not working. I don't know what it causes. Please help me to find out the problems with my code. This is the first time I am dealing with an observer in Magento. So suggest me the best practice while dealing with observers
Thanks in advance..
Best Answer
First of all
adminhtml_customer_save_after
is correct event take a look in\app\code\core\Mage\Adminhtml\controllers\CustomerController.php
.customer_save_after
it is a different event for frontend.But your class definition is wrong. Try this
Also do not forget to clear cache.