1

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

2
  • I think...'events' tag should be declared in 'global' tag not 'adminhtml' tag Commented Sep 4, 2014 at 13:02
  • events tag can come inside global node, adminhtml node and in frontend node. Where should it put depends upon the coditions that we need to achieve :) Commented Sep 4, 2014 at 13:13

2 Answers 2

3

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

<events>
    <adminhtml_customer_save_after>
        <observers>
            <electronicsstore_customization>
                <type>model</type>                           
                    <class>customization/observer</class>
                    <method>saveCustomerCustomizationData</method>
            </electronicsstore_customization>
        </observers>
    </adminhtml_customer_save_after>
</events>

Also do not forget to clear cache.

9
  • thanks for reply. But I have already tried this class also. It didnt worked.. I have copied your code and tried. no result. I think both the class definitions are same and should work. but for me, both of them not working Commented Feb 17, 2014 at 15:28
  • Answer updated. Can you try code again. Commented Feb 17, 2014 at 15:36
  • nope friend its not working. I have tried your code. Is there any other files necessary along with these two files in admin module? Commented Feb 17, 2014 at 15:41
  • Have you defined extension initialisation xml file in app/etc/modules? Commented Feb 17, 2014 at 15:43
  • oh yes and this module is seems to be enable when look into system -> configuration advanced tab Commented Feb 17, 2014 at 15:45
1

You declared your event observer like this:

<events>
    <adminhtml_customer_save_after> <!-- this is wrong-->
        <observers>
            <electronicsstore_customization>
                <type>model</type>                           
       <class>electronicsstore_customization_model_observer</class>
                <method>saveCustomerCustomizationData</method>
            </electronicsstore_customization>
        </observers>
    </adminhtml_customer_save_after>
</events>

The event name is wrong (tag right under <events>). It should be the exact name of the event you are observing. In this case customer_save_after

3
  • :nope its not working. I have tried 'cutomer_save_after' first. But it didnt work. thats why i have used above observer. I have changed it to customer_save_after now and tried again. didnt work. Any other details of module is needed? Commented Feb 17, 2014 at 14:08
  • make sure you cleared the cache.
    – Marius
    Commented Feb 17, 2014 at 15:07
  • nope i have cleared and made a try twice. still no output. it confuses me. Is there is anything I miss in configuration? Commented Feb 17, 2014 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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