Read More
Menu Close

    Use Observer In Magento

    Magento Observers

    In magento Observer  catch magento events and run your function after that event. Magento events like  checkout_cart_product_add_after,checkout_cart_save_after etc you can get more magento events from Magento Events Cheat Sheet (1.7.0.0)

    This way of adding custom event before and after save is useful when we just need to add or do something before or after any Magento core event. For example, setting some values to some attributes before or after product is saved.

    For this, you just need to add add some event observer code in your module’s YourModule/etc/config.xmlfile and add a new observer YourModule/Model/Observer.php

    In the example code below, I have added before and after event hook for customer address and catalog product. Customer address event hooks run before and after saving customer address and catalog product hooks run before and after saving products in admin.

    Searching for an experienced
    Magento Company ?
    Read More

    customer_address_save_before = runs just before saving customer address
    customer_address_save_after = runs immediately after saving customer address

    catalog_product_save_before = runs just before saving product
    catalog_product_save_after = runs just before saving product

    Here’s the code:-

    config.xml

    <global>
        <events>             
            <customer_address_save_before>
                <observers>
                    <yourmodule>
                        <type>singleton</type>
                        <class>yourmodule/observer</class>
                        <method>customer_address_save_before</method>
                    </yourmodule>
                </observers>
            </customer_address_save_before>
    
            <customer_address_save_after>
                <observers>
                    <yourmodule>
                        <type>singleton</type>
                        <class>yourmodule/observer</class>
                        <method>customer_address_save_after</method>
                    </yourmodule>
                </observers>
            </customer_address_save_after>
    
            <catalog_product_save_before>
                <observers>
                    <yourmodule>
                        <type>singleton</type>
                        <class>yourmodule/observer</class>
                        <method>catalog_product_save_before</method>
                    </yourmodule>
                </observers>
            </catalog_product_save_before>
    
            <catalog_product_save_after>
                <observers>
                    <yourmodule>
                        <type>singleton</type>
                        <class>yourmodule/observer</class>
                        <method>catalog_product_save_after</method>
                    </yourmodule>
                </observers>
            </catalog_product_save_after>    
        </events>
    </global>

    Observer.php

    class YourNamespace_YourModule_Model_Observer
    {  
        public function customer_address_save_before($observer)
        {
            $address = $observer->getCustomerAddress();
            //echo "<pre>"; print_r($address->getData()); exit;
            // do something here
        }
    
        public function customer_address_save_after($observer)
        {
            $address = $observer->getCustomerAddress();     
            //echo "<pre>"; print_r($address->getData()); exit;
            // do something here
        }
    
        public function catalog_product_save_before($observer)
        {
            $product = $observer->getProduct();     
            //echo "<pre>"; print_r($product->getData()); exit;
            // do something here
        }
    
        public function catalog_product_save_after($observer)
        {
            $product = $observer->getProduct();     
            //echo "<pre>"; print_r($product->getData()); exit;
            // do something here
        }
    }

    Similarly, you can add your custom function to other events like sales_order_save_before,sales_order_save_aftersales_order_invoice_save_beforesales_order_invoice_save_after, etc.

    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top