Disclaimer: Magenticians does not necessarily agree with the views expressed in this guest post. They are presented to bring to light all diverse views in the Magento and general ecommerce community.
There are several free and paid Magento modules available online. However, if you need to make changes or the module is not fulfilling your requirement for more functionalities, you should have basic knowledge of how they are created and how they work, etc. For example, you should know where to place the files, what is the sequence of calling methods and functions or scripts, and so on. Hence, we are starting this basic tutorial on how to create a Magento custom module which will help us understand the structure of Magento 1.x modules and you can learn to modify them to suit your needs. In this tutorial, you will be introduced to the coding mechanism of Magento modules which will simply help you write a log file every time a product is saved.<?xml version="1.0"?> <config> <modules> <Mage_ProductLogUpdate> <version>0.0.1</version> </Mage_ProductLogUpdate> </modules> </config><config> Root node for Magento module configuration. <modules> Contains basic information about Magento module. <Mage_ProductLogUpdate> NameSpace_ModuleName This should be exactly the same as the namespace and the module name separated by an underscore. <version> Version of module.
<?xml version="1.0"?> <config> <modules> <Mage_ProductLogUpdate> <active>true</active> <codePool>local</codePool> </Mage_ProductLogUpdate> </modules> </config><active> Whether our module is active: true or false. <codePool> Which code pool to use: core, community or local. Now we have a fully functional module enabled in Magento. To check it out, log into the Magento Admin Panel and navigate to System > Configuration > Advanced > Advanced and view the Disable Module Output listing. We should see our Mage_ProductLogUpdate module. If not, carefully run through the steps again and diagnose the mistakes. Now, our module’s structure looks like:
<?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><global> Configure our module’s behavior in the global scope. <events> Defining an event observer. <catalog_product_save_after> The code of the event we want to observe. <observers> Defining an observer for this event. <mage_productlogupdate> Unique identifier within the catalog_product_save_after node. By convention, we write the module’s name in lowercase. <class> The model to be instantiated. <method> The method of the class to be called. <type> The type of class to instantiate.
<?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> Defining models. <mage_productlogupdate> Unique identifier in the model’s node. By convention, we put the module’s name in lowercase. <class> The path to our model’s directory, with directory separators replaced by underscores. Now we will create our model which will be instantiated when the event is dispatched. Create a new PHP file in app/code/local/Mage/ProductLogUpdate/Model/Observer.php with the following content:
<?php /* Our class name should follow the directory structure of our Observer.php model, starting from the namespace, replacing directory separators with underscores. The directory of ousr Observer.php is following: app/code/local/Mage/ProductLogUpdate/Model/Observer.php */class Mage_ProductLogUpdate_Model_Observer { // Magento passes a Varien_Event_Observer object as the first parameter of dispatched events. public function logUpdate(Varien_Event_Observer $observer) { // Retrieve the product being updated from the event observer $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'); } } ?>
Looking for the most reliable hosting platform?
Then, do check out Managed Magento hosting and avail Free Trial for 3 Days.
Your greatest possible competitive advantage can be your clients and the interactions they have with… Read More
Digital marketing KPIs are measurable values that marketing teams use to track progress toward desired… Read More
In today's digital age, fraud poses a significant threat to businesses of all sizes. As… Read More
Financial crimes continue to evolve and proliferate in our increasingly digital, global economy. From complex… Read More
In the highly competitive modern workplace, trust, and employee loyalty are crucial factors for long-term… Read More
In the ever-evolving world of small business developing and implementing effective marketing strategies is critical to… Read More
View Comments
Really good article! I adapted the code to create a module with another Magento event and everything works fine!
Thx
Your Welcome! :)
good example to understand basics
Thanks!
awesome, simpler than all i have found in the last 24 hours, thanks a bunch for this
Thanks for your appreciation and suggestion!
Simple but much needed for nosing into the magento system
Thanks
Hey..
I want to know about uninstall script of magento custom module..
for example at the installation of custom module i have created custom attribute of product
$installer = Mage::getResourceModel('catalog/setup', 'catalog_setup');
$installer->startSetup();
$installer->addAttribute(
'catalog_product',
'_length',
array(
'type' => 'decimal',
'label' => 'Length',
'required' => false,
'visible_on_front' => true,
'sort_order' => 26
)
);
$installer->endSetup();
how to remove this attribute when my custom module is uninstalled from magento connect.
please help
Your question is not related to the above article. But I will look in to the matter and if I find any solution, I will let you know. Thanks!
Thanks for your response.
i have found some shell scripts for this purpose but i want to execute them at the time of custom module uninstall.
That's a neat article you got, it's a shame that support for Magento 1.x will stop next year. Any more resources you could point out for developing modules for 1.x ? Cheers!
not working for me
Hi Rohit,
You might be missing something. Please copy paste all the codes into the appropriate files and try again. Thanks!
Nice. It's working!!! Thank you for your job.
You're welcome! :)
Hello sir actully i am editing mu configure.phtml in adminhtml....but i want add my min.js in this (configure.phtml) page then how can i add my js.
you want this in Magento 2 or Magento 1?
Hi, Its not working for me. I use the same code for my Magento ver. 1.9.3.8 but it will raised the warning. I have used the Module Namespace: Narayan.
2018-04-16T06:42:16+00:00 ERR (3): Warning: include(MageNarayanProductlogupdateModelObserver.php): failed to open stream: No such file or directory in D:ApachehtdocsmagentolibVarienAutoload.php on line 94
2018-04-16T06:42:16+00:00 ERR (3): Warning: include(): Failed opening 'MageNarayanProductlogupdateModelObserver.php' for inclusion (include_path='D:Apachehtdocsmagentoappcodelocal;D:Apachehtdocsmagentoappcodecommunity;D:Apachehtdocsmagentoappcodecore;D:Apachehtdocsmagentolib;.;C:phppear') in D:ApachehtdocsmagentolibVarienAutoload.php on line 94