The Basics Of Creating A Magento Module

Posted on Friday, Oct 11 2019 @ 19:22 CEST by Thomas De Maesschalck
There are a lot of modules and extensions to the free and complete Magento online store organization system developed by the community on the Internet(at mageworx marketplace for example), but what if they don't all meet your requirements? What if you wanted to understand the structure of Magento modules a little better, to have the skills and capabilities to modify them or even write your own modules from scratch?

In this guide, we will dedicate you to the process of developing modules for the Magento system. The purpose of the module will be to be able to record information in a log file every time the product is saved. This simple module will allow us to cover some interesting topics, including

  • Application/Code directories,
  • Structure and creation of Magento modules,
  • Event observers,
  • Log file management.

    Before you start
    This manual requires you to have Magento installed and running, either on your local computer or on a supposed server to which you can add files. The Magento version does not really matter, as we will only cover the fundamental aspects that are present in all versions and builds: Community, Professional, and Enterprise.

    Disabling caching
    The first lesson every Magento developer should remember is: disable all caching! This can be done by going to the Admin Panel > System > Cache Management > Select All > Actions: Disable > Submit menu.

    Although the cache is very convenient and productive in terms of speeding up performance, it still contradicts the development process. You can leave it on at your own risk. Every Magento developer we've met has had a few problems with the changes made over the years without being displayed on the site for hours. This is because the system displayed the latest version saved in the cache.

    Module configuration
    Next, we will proceed to configure our module. The configuration file is located inside our module in the directory called "etc". We need to create a new XML file: app/code/local/SmashingMagazine/LogProductUpdate/etc/config.xml. This file will inform Magento about the location of our module's files, as well as other version data and events to be monitored. Now we need to create a simple config.xml file that will contain comments explaining the values of each partition.

    Activate our module
    Next, we must inform Magento about the presence of our module. This is done by creating a new XML file in app/etc/modules. The name of this file can be at your discretion, as Magento will read all XML files in the directory and will be interested only in their contents. However, the logic and rules require that we use the same module and file name. Let's create an app/etc/modules/SmashingMagazine_LogProductUpdate.xml

    Verification: Is the module activated?
    We now have a fully functional module activated in the Magento system. It does not produce anything, but it is a valid working module. This allows us to make sure that we have done everything right so far. If we log in to the Magento admin panel and go to the System > Configuration > Advanced > Advanced menu and see the Disable Modules Output list, we will see our SmashingMagazine_LogProductUpdate module is active. If it isn't on the list, we've done something wrong, so we'll have to do it all over again with more precision. This is where new developers get to know the cache in Magento!

    We define a reviewer of events
    Event Viewers are very powerful tools that provide the easiest and most affordable way to extend the functionality of Magento without having to rewrite any code or classes. We need to keep track of events that occur in Magento right after the items are saved, so the code we need is contained in catalog_product_save_after. We need to understand Magento's model structure to determine the event code we need when defining a new browser. Only this is not included in our current article. Next time we will tell you about it!

    Configuring the model directory
    In the Event Viewer, we have defined above, we can refer to a model we have not yet created. We need to update the config.xml file to inform Magento where we can find our module models.

    Creating a browser model
    We will now create a model that will assign the appropriate values to the event. Create a new PHP file in app/code/local/SmashingMagazine/LogProductUpdate/Model/Observer.php

    That's all it is! You can test it.
    Now that our module is complete, we can try it out! Log in to the Magento admin panel, create and update a product in your directory, then check the var/log folder and you will see that the product-updates.log file is filled with information.