Take the 2-minute tour ×
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It's 100% free, no registration required.

I trying make event observer with save_after.
This is an attempt to set Meta Title automatically when saving my products.
Creating a module:
So looks like the folder structure:
enter image description here
Observer.php code:

<?php    
class Unnamed_MyModule_Model_Observer
    {
        public function catalog_product_save_after($observer)
        {
            $product = $observer->getProduct();
            if(!$product->getMetaTitle()){
                $name = $product->getName();           
                $metaTitle = str_replace(' - ', ' ', $name);
                $product->setMetaTitle($metaTitle);
                $product->getResource()->saveAttribute($product, 'meta_title');
            }
        }
    } 

config.xml code:

<?xml version="1.0"?>
<global>
    <events>          
        <catalog_product_save_after>
            <observers> 
                <unnamed_mymodule>
                    <type>singleton</type>
                    <class>Unnamed/MyModule</class>
                    <method>catalog_product_save_after</method>
                </unnamed_mymodule>
            </observers> 
        </catalog_product_save_after>
    </events> 
</global>
</code>


magento/app/etc/modules Unnamed_MyModule.xml code:

<config>
    <modules>
        <Unnamed_MyModule>
            <active>true</active>
            <codePool>local</codePool>
        </Unnamed_MyModule>
    </modules>
</config>

It seems to me that there are no errors. But It is not work. optional: magento 1.9, CE, I'm a beginner.
Tell me what is wrong?
Thanks!

share|improve this question

4 Answers 4

up vote 1 down vote accepted

There issue with config.xml ,now check config.xml code:

<?xml version="1.0"?>
<config>
  <modules>
    <Unnamed_MyModule>
      <version>0.1.0</version>
    </Unnamed_MyModule>
  </modules>
    <global>
        <models>
            <mymoduleunnamed>
                <class>Unnamed_MyModule_Model</class>
            </mymoduleunnamed>
        </models>
  </global>
    <global>
        <events>
            <catalog_product_save_after>
                <observers>
                    <mymoduleunnamed>
                        <type>singleton</type>
                        <class>mymoduleunnamed/observer</class>
                        <method>catalog_product_save_after</method>
                    </mymoduleunnamed>
                </observers>
            </catalog_product_save_after>
        </events>
    </global>       
</config> 
share|improve this answer

Well, from your configuration i see that you haven't entered correct observer name in configuration, so you should enter a full observer's class name or short:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <global>
     <events>          
        <catalog_product_save_after>
            <observers> 
                <unnamed_mymodule>
                    <type>singleton</type>
                    <!--  using full name -->
                    <class>Unnamed_MyModule_Model_Observer</class> 
                    <method>catalog_product_save_after</method>
                </unnamed_mymodule>
            </observers> 
        </catalog_product_save_after>
      </events> 
    </global>           
</config>

Try it. Clean cache before!

share|improve this answer

So you have two options.

  1. Define your model name under the <global> node,
  2. Set the full path to the observer on the event,

Define model

<models>
    <your_module>
        <class>Your_Module_Model</class>
    </your_module>
</models>

Then update the class on the event to match:

<class>your_module/observer</class>

Set the full path

Simply update the class part of your event to match the full class name of your observer:

<class>Your_Module_Model_Observer</class>
share|improve this answer

It thought,it will good idea to call catalog_product_save_before event instead of catalog_product_save_after.

Modified config.xml value is

<?xml version="1.0"?>
<config>
  <modules>
    <Unnamed_MyModule>
      <version>0.1.0</version>
    </Unnamed_MyModule>
  </modules>
    <global>
        <models>
            <mymoduleunnamed>
                <class>Unnamed_MyModule_Model</class>
            </mymoduleunnamed>
        </models>
  </global>
    <global>
        <events>
            <catalog_product_save_before>
                <observers>
                    <mymoduleunnamed>
                        <type>singleton</type>
                        <class>mymoduleunnamed/observer</class>
                        <method>catalog_product_save_after</method>
                    </mymoduleunnamed>
                </observers>
            </catalog_product_save_before>
        </events>
    </global>       
</config> 

Observer.php code is:

<?php    
class Unnamed_MyModule_Model_Observer
    {
        public function catalog_product_save_after($observer)
        {
            $product = $observer->getEvent()->getProduct();;
            if(is_null($product->getData('meta_title')){
                $name = $product->getData('name());           
                $metaTitle = str_replace(' - ', ' ', $name);

                //$product->setMetaTitle($metaTitle);
                //$product->getResource()->saveAttribute($product, 'meta_title');
        $product->setData('meta_title',$finalTemWeight);
            }
        }
    }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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