2

I need to call our own custom function after save, delete, add products in Magento. I need to call in the separate file that is located in Magento root folder.

4

Magento has events for every controller action, both before and after executing. In your case you'll want to use catalog_product_delete_after and catalog_product_save_after.

I'm not really sure what you mean by add tho, is that for a new product? In that case you can observer ...save as well.

It would look something like this in your config.xml

<?xml version="1.0"?>
<config>
  <global>
    <models>
        <module]>
             <class>[Namespace]_[Module]_Model</class>
        </[module]>
    </models>
    <events>
      <catalog_product_save_after>
        <observers>
          <[namespace]_[module]_save_observer>
            <type>singleton</type>
            <class>[Namespace]_[Module]_Model_Observer</class>
            <method>save</method>
          </[namespace]_[module]_save_observer>
        </observers>
      </catalog_product_save_after>
      <catalog_product_save_delete>
        <observers>
          <[namespace]_[module]_delete_observer>
            <type>singleton</type>
            <class>[Namespace]_[Module]_Model_Observer</class>
            <method>delete</method>
          </[namespace]_[module]_delete_observer>
        </observers>
      </catalog_product_save_delete>     
    </events>
  </global>
</config>

For more info on observers please check this wikipage

  • .Can we do this in separate file at magento folder without making any module – ZOE RULE Oct 10 '13 at 10:39
  • You might be able to add it to the etc/global.xml, since all XMLs are concatenated but I would advise you to make an extension. It's not that much work and a whole lot cleaner – Sander Mangel Oct 10 '13 at 10:57
  • Thanks a lot.You saved a lot of time .Thank you very much .I will do it by making a small extension – ZOE RULE Oct 10 '13 at 11:05
  • Can you add me on my personal gmail id so that i will be in contact you for my help? – ZOE RULE Oct 10 '13 at 11:07
  • I normally don't give out my contact details. If you have a question you can always post it on this forum, I'm here pretty much all the time :) – Sander Mangel Oct 10 '13 at 11:08
2

Use the default events for these action catalog_product_save_after (and others), and create an observer that has some methods that act as wrappers for your custom functions. Something like:

<?php
require_once Mage::getBaseDir().'your_functions.php';
class Company_Module_Model_Observer{
    public function catalog_product_save_after($observer){
        $product = $observer->getEvent()->getProduct();
        yourFunction($product);//call function from your external file
    }
   //all other methods here
}

Even if this should work, I don't approve of this kind of code. You should keep the Magento standards and have your functions inside an observer.

  • Thanks for help.Am I need to make a module or I can call observer directly in file that is at magento root folder. – ZOE RULE Oct 10 '13 at 10:37
  • @deepu. If you take this approach then you need to create a new module. But before doing so, take a look at the answer provided by SanderMangel. it has more code in it that should help you. – Marius Oct 10 '13 at 10:46
  • ok thanks.I will do it by making a new module – ZOE RULE Oct 10 '13 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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