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
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.
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.
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.