Magento 1.7/1.8 – How to Update Product Attribute Value After Save

adminmagento-1.7magento-1.8productproduct-attribute

I wanna update product attribute value whenever new product will be added from admin in Magento 1.8. i used event observers but nothing happen my observer method is not calling when new product added i used following code in config.xml

I tried below event in both global and admin section

<?xml version="1.0"?>

<config>
    <modules>
        <Nextgeni_MostLove>
            <version>0.1.0</version>
        </Nextgeni_MostLove>
    </modules>

     <global>
        <models>
            <mostlove>
                <class>Nextgeni_MostLove_Model</class>
                <resourceModel>mostlove_mysql4</resourceModel>
            </mostlove>
            <validationmodel>
                <class>Nextgeni_MostLove_Model</class>
            </validationmodel>
            <mostlove_mysql4>
                <class>Nextgeni_MostLove_Model_Mysql4</class>
                <entities>
                    <mostlove>
                        <table>most_love_product</table>
                    </mostlove>
                </entities>
            </mostlove_mysql4>
        </models>

        <helpers>
            <mostlove>
                <class>Nextgeni_MostLove_Helper</class>
                <!-- This will be the path of Helper Files -->
            </mostlove>
        </helpers>

        <blocks>
            <mostlove>
                <class>Nextgeni_MostLove_Block</class>
            </mostlove>
            <catalog>
                <rewrite>
                    <product_featured>Nextgeni_MostLove_Catalog_Block_Product_Featured</product_featured>
                </rewrite>
            </catalog>          
        </blocks>

         <resources>
            <mostlove_setup>
                <setup>
                    <module>Nextgeni_MostLove</module>
                    <class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </mostlove_setup>
            <mostlove_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </mostlove_write>
            <mostlove_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </mostlove_read>
        </resources>

        <events>
            <catalog_product_save_before>
                <observers>
                    <magedev_events_observer>
                        <type>singleton</type>
                        <class>Nextgeni_MostLove_Model_Observer</class>
                        <method>saveProductBefore</method>
                    </magedev_events_observer>
                </observers>
            </catalog_product_save_before>
        </events> 
    </global>

    <frontend>
        <routers>
            <mostlove>
                <use>standard</use>
                <args>
                    <module>Nextgeni_MostLove</module>
                    <frontName>mostlove</frontName>
                </args>
            </mostlove>
        </routers>

        <layout>
            <updates>
                <mostlove>
                    <file>mostlove.xml</file>
                </mostlove>
            </updates>
        </layout>
    </frontend>
    <default>
        <mostlove>
            <most_love_config>
                <showimage>1</showimage>    
                <displayText>Love</displayText>     
                <productLoveMin>0</productLoveMin>              
                <productLoveMax>0</productLoveMax>  
                <enableHeart>heart_icon.png</enableHeart>
                <disableHeart>heart_disable.png</disableHeart>
              </most_love_config>
        </mostlove>
    </default>

</config>

My Observer file code is

<?php
class Nextgeni_MostLove_Model_Observer
{



     public function catalog_product_save_before(Varien_Event_Observer $observer)
    {
        $product = $observer->getProduct();
        Mage::throwException(Mage::helper('adminhtml')->__('You totally failed at that.'));
        echo "<pre>"; print_r($product->getData()); exit;
    // do something here
    }

    public function applyAllRulesOnProduct($observer)
    {
        $product = $observer->getProduct();
        Mage::throwException(Mage::helper('adminhtml')->__('You totally failed at that.'));
        echo "<pre>"; print_r($product->getData()); exit;
    // do something here
    }
}

Best Answer

You shouldn't have left out <!-- Some code here-->. I'd like to see the <models> part inside <global>. I expect the error to be there. Try to change <class>Nextgeni_MostLove/observer</class> to <class>Nextgeni_MostLove_Model_Observer</class> or whatever the class of your observer is.

Another potential error: the <events> part should be inside <global>, <frontend> (not in your case) or <adminhtml>.