Creating a Basic Module in Magento – Final – Part 4

We’re definitely in the fourth and final part of creating Magento modules.

Magento Modules – Event Observer

Here we’ll use “Event Observer” to extend Mage Functions. By using “Event Observer” we can easily rewrite functions, core and classes etc. As earlier we have discussed, the module will save a log every time we add a new product. So, here we would like to mention the code is “catalog_product_save_after”.

Here you can learn More about us . Next don’t miss our exciting offers on Magento Hosting plans. For more info on our plans and services feel free to contact us. Aaspiration Hosting means more freedom to Magento. 

Tanzia Farin Chy for Aspiration Hosting

Simply we are going to edit config.xml file to insert the Event Observer:

<?xml version="1.0"?>
<config>
<modules>
<Mage_ProductLogUpdate>
<version>0.0.1</version>
</Mage_ProductLogUpdate>
</modules>
<global>
<events>
<catalog_product_save_after>
<observers>
<mage_productlogupdate>
<class>mage_productlogupdate/observer</class>
<method>logUpdate</method>
<type>singleton</type>
</mage_productlogupdate>
</observers>
</catalog_product_save_after>
</events>
</global>
</config>
  • Here is the list of code functionalities:
  • global – Controls Magento modules behaviors in global scope.
  • Events – Defines the Event Observers.
  • catalog_product_save_after – The Code we’re using for our new log module.
  • Observers – Defines an observer for a specific event.
  • mage_productlogupdate – Particular identifier in the node “catalog_product_save_after” .
  • class – The model selected to present.
  • Method – Used to call a class.
  • Type – Class type.

Magento Modules – Model’s DIR

As we already made a reference logUpdate in our model’s class “mage_productlogupdate/observer”. Here we’ll inform Mage by editing config.xml about the model of our module. So, use the following codes to do so.

<?xml version="1.0"?>
<config>
<modules>
<Mage_ProductLogUpdate>
<version>0.0.1</version>
</Mage_ProductLogUpdate>
</modules>
<global>
<models>
<mage_productlogupdate>
<class>Mage_ProductLogUpdate_Model</class>
</mage_productlogupdate>
</models>
<events>
<catalog_product_save_after>
<observers>
<mage_productlogupdate>
<class>mage_productlogupdate/observer</class>
<method>logUpdate</method>
<type>singleton</type>
</mage_productlogupdate>
</observers>
</catalog_product_save_after>
</events>
</global>
</config>
  • models – Defines model.
  • mage_productlogupdate – In model’s not particular identifier.
  • class – Model’s directory path.

In this step the model. It will instantiate after the event is dispatched. For the same reason, we’ll create a PHP file under app/code/local/Mage/ProductLogUpdate/Model/Observer.php. Hence the file includes the following codes:

<?php
/* The class name should lookks like the directory structure of Observer.php model. Let's begin from the namespace and rewrite DIR separators using underscores. Here is the directory of our Observer.php :
app/code/local/Mage/ProductLogUpdate/Model/Observer.php */
class Mage_ProductLogUpdate_Model_Observer
{
// As the first parameter of dispatched events, Mage passes a Varien_Event_Observer object .
public function logUpdate(Varien_Event_Observer $observer)
{
// Here we'll Retrieve using the event observer, the product being updated. 
$product = $observer->getEvent()->getProduct();
// Write a new line to var/log/product-updates.log
$name = $product->getName();
$sku = $product->getSku();
Mage::log("{$name} ({$sku}) updated", null, 'product-updates.log');
}
}
?>

Magento Modules – Conclusion

Here we’re done with creating a basic module. Now, log in to the Mage Admin Panel. Next create a new product to test the module. Finally, check the updates.log file under the following path “var/log”.

Hence make sure the module is working properly. In case any issue arises check and confirm Mage provides enough permission to write in to the Directory. Now we’ll also need to check that log settings is turned on.

Next go to the following to enable the settings:

Admin Panel > System > Configuration > Developer > Log Settings > Enabled

In sum, the Directory structure of the our module is looks like following:

magento modules

Currently Viewing Part 4 |Go to Part 3| Go to Part 2 | Go to Part 1