MAGENTO 1.X – HOW TO CREATE AN OBSERVER FROM SCRATCH


In some cases, you would need to add some custom functionality inside Magento flow, for those ones, Magento provide several events where you can insert your custom code in a custom extension. To do so, you need to perform the following steps:

  • Create a new module xml file at app/etc/modules/Vendor_ObserverTestExt.xml
  • Create the following folder structure for your module:
    app/code/local/Vendor/ObserverTestExt/
    app/code/local/Vendor/ObserverTestExt/etc/
    app/code/local/Vendor/ObserverTestExt/Model/
  • Create your module configuration file:
    On folder app/code/local/Vendor/ObserverTestExt/etc create a config.xml file with the following content:

    Notes:
    <callobservertestext> is a unique identifier of our observer’s folder, inside it, the tag <class> represents our observer folder, and the prefix of our observer class.

    <events> Here we are going to set up each event where we want to call a custom method (eg: sales_order_place_after). A nice cheat of all events is this one: https://www.nicksays.co.uk/magento-events-cheat-sheet-1-9/

    <vendor_observertestext_model_observer> This is a unique identifier of our observer, it’s very common to use the observer class name.
    Inside it, the <class> tag can be set as two different ways:
    * Class name: Vendor_ObserverTestExt_Model_Observer.
    * folderidentifier/filename: callobservertestext/observer.

  • Create the Observer.php file inside app/code/local/Vendor/ObserverTestExt/Model with the following content:

     
  • Finally, lets clean cache, make a complete purchase order(just for this example, and to raise the extended event) and check out your log file in var/log.