Hire Magento Module Creator That Knows Basics of Development

commas-left

Do you know basic tips for developing Magento module?

Have you ever thought small mistakes resulted into big disaster for your development task?

Looking the importance of basics of module development, we will introduce Magento coding in the form of a “Hello World”-style module.

commas-right

Magento Module Creator

Module works with an aim to write some information to a log file every time a product is saved. Different types of interesting topics will be covered under basic of modules such as:

  • The structure and creation of a Magento module,
  • The app/code directories,
  • Event observers,
  • Logging.

We assume that you already have installed Magento up and running, either locally or on development service in which you have to add new files. It totally doesn’t matter what version you are using as it covers fundamental aspects that have across all versions and editions: Community, Professional and Enterprise.

Disable The Cache

This is one of the most important lessons a Magento developer should learn, i.e. disable the cache! For that you have to go to Admin Panel > System > Cache Management > Select All > Actions: Disable > Submit.

The cache is creators’ enemy while they are boosting performance in a production environment. Among all, 90% of Magneto module developers are spending more than 1 hour for finding latest update as it doesn’t show to them. Magneto displays website’s version that expediently cached earlier that day.

The app/code Directory

Creators will find brains of Magento in individual modules inside the app/code directory that divided into three areas: core, community and local.

Core
All of the functionality for products, categories, customers, payments, etc. is included in the

1
app/code/core

directory. It is advisable to keep

1
app/code/core

off limits until you know what you are doing as such files should not be changed. One of the best things about Magento is you can change the functionality of any of these core files without changing them directly. This will ensure you that your application remains upgrade-proof.

Community
In

1
app/code/community

, you will find modules that provided by third parties. Developers can download many modules from Magento Connect and can install through the built-in “Package Manager.”

Local
Magento is available with empty

1
app/code/local

directory in which you can add bespoke modules for your own Magento installation. In this section, you will work for long duration of this tutorial.

Structuring Our Directory

Developers have to open editor and navigate to

1
app/code/local

in order to add some new directories and files.

Module Namespace
A “namespace” is the first directory that can be called anything you want to. “Mage” is used by Magento as its namespace. For this tutorial, we will use “Perceptionsystem” as our namespace. So, create the directory

1
app/code/local/Perceptionsystem

.

Module Name
Now, it’s time give a descriptive name to our module. As module will have log entries each time a product is saved, it is must to have a logical name, i.e.

1
LogProductUpdate

. Create the directory

1
app/code/local/Perceptionsystem/LogProductUpdate

.

We have to following below given structure for module. As the directory and file names are case-sensitive, it is advisable to capitalize where appropriate.

1
2
3
4
5
app
  - code
      - local
          - Perceptionsystem
              - LogProductUpdate

Configuring Our Module

In next step, you have to configure module. Developers can find configuration files inside a directory named etc that developed together with a XML file:

1
app/code/local/ Perceptionsystem/LogProductUpdate/etc/config.xml

.

This XML file gives information about the location of the files in modules and many other things like events, version number and so on. In next step, we are going to create a simple

1
config.xml

file that has comments that explains means of each section.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0" encoding="UTF-8"?>

<!-- The root node for Magento module configuration -->
<config>

    <!--
        The module's node contains basic
        information about each Magento module
    -->
    <modules>

        <!--
            This must exactly match the namespace and module's folder
            names, with directory separators replaced by underscores
        -->
        <Perceptionsystem_LogProductUpdate>

            <!-- The version of our module, starting at 0.0.1 -->
            <version>0.0.1</version>

        </Perceptionsystem_LogProductUpdate>

    </modules>

</config>

Activating Our Module

In next step, we are developing a new XML file in

1
app/etc/modules

with any name you want to put. As Magento will read all XML files in this directory and can also be interested on content only. But, by convention we should put the name of module and file same. Now, create

1
app/etc/modules/Perceptionsystem_LogProductUpdate.xml

with the following content:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Perceptionsystem_LogProductUpdate>

            <!-- Whether our module is active: true or false -->
            <active>true</active>

            <!-- Which code pool to use: core, community or local -->
            <codePool>local</codePool>

        </Perceptionsystem_LogProductUpdate>
    </modules>
</config>

Check Whether Module Is Enabled or Not

Now, we have a fully functional enabled module. It doesn’t do anything but can be considered as valid module. It is first chance for you to see whether you have correctly configured everything or not.

We should see

1
Perceptionsystem_LogProductUpdate

module listed as enabled when we log into Magento admin panel and navigate to System > Configuration > Advanced > Advanced and view the

1
Disable Modules Output

listing.

If you don’t found then something has surely gone wrong. So, it is advisable to be careful while stepping up point again. You will find module’s structure like this:

1
2
3
4
5
6
7
8
9
10
11
app
  - code
      - local
          - Perceptionsystem
              - LogProductUpdate
                  - etc
                      - config.xml

  - etc
      - modules
          - Perceptionsystem_LogProductUpdate.xml

Defining An Event Observer

With the help of Event observers, one can extend Magento’s functionality without having to rewrite or override any core methods or classes in the cleanest ways. Now, we have to observe the event that dispatched by Magneto after a product is saved. So,

1
catalog_product_save_after

is the code for the event that we actually are interested in it.

A basic understanding of Magento’s model layer is needed to determine event code to use when defining a new observer. In next step, you have to change

1
config.xml

to include the event observer definition:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Perceptionsystem_LogProductUpdate>
            <version>0.0.1</version>
        </Perceptionsystem_LogProductUpdate>
    </modules>

    <!-- Configure our module's behavior in the global scope -->
    <global>

        <!-- Defining an event observer -->
        <events>

            <!-- The code of the event we want to observe -->
            <catalog_product_save_after>

                <!-- Defining an observer for this event -->
                <observers<

                    <!--
                        Unique identifier within the
                        catalog_product_save_after node.
                        By convention, we write the module's
                        name in lowercase.
                    -->
                    <perceptionsystem_logproductupdate>

                        <!-- The model to be instantiated -->
                        <class>perceptionsystem_logproductupdate/observer</class>

                        <!-- The method of the class to be called -->
                        <method>logUpdate</method>

                        <!-- The type of class to instantiate -->
                        <type>singleton</type>

                    </perceptionsystem_logproductupdate>

                </observers>

            </catalog_product_save_after>

        </events>

    </global>

</config>

Configuring Our Model’s Directory

We have created reference to a model in above defined event that we haven’t yet created. We have to inform Magento, where to search out models by

1
updatingconfig.xml

with the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Perceptionsystem_LogProductUpdate>
            <version>0.0.1</version>
        </Perceptionsystem_LogProductUpdate>
    </modules>

    <!-- Configure our module's behavior in the global scope -->
    <global>

        <!-- Defining models -->
        <models>

            <!--
                Unique identifier in the model's node.
                By convention, we put the module's name in lowercase.
            -->
            <perceptionsystem_logproductupdate>

                <!--
                    The path to our models directory, with directory
                    separators replaced by underscores
                -->
                <class>Perceptionsystem_LogProductUpdate_Model</class>

            </perceptionsystem_logproductupdate>

        </models>

        <events>
            <catalog_product_save_after>
                <observers>
                    <perceptionsystem_logproductupdate>
                        <class>perceptionsystem_logproductupdate/observer</class>
                        <method>logUpdate</method>
                        <type>singleton</type>
                    </perceptionsystem_logproductupdate>
                </observers>
            </catalog_product_save_after>
        </events>

    </global>

</config>

Creating An Observer Model

Now, we are going to create model that to be instantiated when the event is dispatched. In order to create a new PHP file in

1
app/code/local/PerceptionSystem/LogProductUpdate/Model/Observer.php

with the

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
/**
 * Our class name should follow the directory structure of
 * our Observer.php model, starting from the namespace,
 * replacing directory separators with underscores.
 * i.e. app/code/local/PerceptionSystem/
 *                     LogProductUpdate/Model/Observer.php
 */
class PerceptionSystem_LogProductUpdate_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'
        );
    }
}

Almost Everything is Done

You will find directory structure like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
app
  - code
      - local
          - Perceptionsystem
              - LogProductUpdate
                  - Model
                      - Observer.php

                  - etc
                      - config.xml

  - etc
      - modules
          - Perceptionsystem_LogProductUpdate.xml

This module is completed now. Now, we have try it out by logging into the Magento admin panel, create or update a product in catalog, and then check the

1
var/log

folder to see

1
product-updates.log

file populated.

Magento module creator has to ensure that the correct permissions are set to allow Magento to write to this directory, if nothing seen or the directory does not exist and that logging is enabled in Admin Panel > System > Configuration > Developer > Log Settings > Enabled.

With the help of this basic tutorial we have shown an overall understanding of how Magento modules work. Once, you have completed this tutorial, it is advisable to explore the Magento modules in

1
app/code/core

and look if you have better idea of working or not.

Do you want to ask any question about module development to our Magento module creator? Feel free to contact us through comment section.

work with us

Leave a Reply

Your email address will not be published. Required fields are marked *

*