Creating a Custom Magento Module

Magento is an open-source e-commerce platform that comes in two different variants:

  • Magento Enterprise Edition: This version basically targets to help small and medium businesses.
  • Magento: This free to download version of  Magento is used by many web developers to provide e-commerce solutions for their clients.

Magento being very versatile, comprises loads of useful features and supports both free and paid modules. It supports modules that can be installed online as well as can be customized in order to meet the client’s requirement(s). Customization is achieved by creating a module and then can be modified according to the choice using the power of PHP language.

In this post we will learn about the basics of creating a custom Magento module.

So we will start by creating a Hello World module.

Where to put our module ?

All the Magento modules are located in the /app/code/ folder and are divided into three sections known as the code pools i.e core, community, local.

/app/code

|- /core/

|- /community/

|- /local/

core – It holds the modules that are present in the base Magento and it forms the core functionality that can be found in the  /app/code/core/ folder.

community – It holds modules that are developed by the third-parties and will be put into the /app/code/community/ folder.

local – This folder is created empty during Magento installation that can be used for custom modules on a. If not present then it can be created manually in the folder /app/code/local/. All the development is generally performed here.

Disabling the cache memory:

The first thing we need to do is disable the cache because if not done, all the changed data will not be shown immediately and it will slow down the development process. Cache can be disabled by going to Admin Panel -> System -> Cache Management -> Select All-> Action: Disable-> Submit.

Magento module structure:

All Magento modules consist of two main sections: module folder and basic configuration file.

Let us start from the module folder:

At first the namespace folder is created. The name should not contain any spaces or underscores. Usually, a company’s name or a developer’s name can be used as the namespace. So, for example here we have  used “Insync” as the namespace under which a custom module will be created.

Now we have a folder as  /app/code/local/ Insync.

[imageframe lightbox=”yes” style_type=”none” bordercolor=”” bordersize=”0px” stylecolor=”” align=”none” link=”” linktarget=”_self” animation_type=”0″ animation_direction=”down” animation_speed=”0.1″ class=”” id=””][/imageframe]

After this we have to choose a module name and an appropriate folder in our namespace is created. Let us  call it “HelloWorld”. Module name and namespace are case-sensitive so care should be taken while naming them.

Now we should get a folder structure like this

/app

|- /code

|- /local

|- / Insync

|- / HelloWorld

[imageframe lightbox=”yes” style_type=”none” bordercolor=”” bordersize=”0px” stylecolor=”” align=”none” link=”” linktarget=”_self” animation_type=”0″ animation_direction=”down” animation_speed=”0.1″ class=”” id=””][/imageframe]

The Magento module structure consists of six main folders: etc, Block, Model, Helper, controllers and sql.

etc –All the modules’ configuration xml files are to be placed here where each module has a required  config.xml file.

Block – This folder is used for storing the View classes. The Main goal of these classes is to link the Models’ data with the template files from the Magento themes.

Model –Generally, Models are designed for connecting to the database and processing the data.

Helper –Helper classes that contain different utility methods can be called from any point of Magento (blocks, models etc.). Helper methods are called this way Mage::helper(‘modulename/helpername’), where each module has got a  default helper class Helper/Data.php (in this case it will be  /Insync/HelloWorld/Helper/Data).

controllers – This folder contains the controller classes, that provide all the business logic of our module.

sql – Files that are processed on the module installation are located in this folder.

Configuring and activating module:

Next step the etc/config.xml file is created. Its goal it to inform the Magento about the module name and version, file’s location, events, resources used by module etc. So, we will create the etc folder and config.xml file in it.

[imageframe lightbox=”yes” style_type=”none” bordercolor=”” bordersize=”0px” stylecolor=”” align=”none” link=”” linktarget=”_self” animation_type=”0″ animation_direction=”down” animation_speed=”0.1″ class=”” id=””][/imageframe]

Then we need to activate our custom module. All the files, indicating which modules are active and in what order will they be loaded, are located in /app/etc/modules. So let us  create a file as /app/etc/modules/Insync_HelloWorld.xml.

[imageframe lightbox=”yes” style_type=”none” bordercolor=”” bordersize=”0px” stylecolor=”” align=”none” link=”” linktarget=”_self” animation_type=”0″ animation_direction=”down” animation_speed=”0.1″ class=”” id=””][/imageframe]

[imageframe lightbox=”yes” style_type=”none” bordercolor=”” bordersize=”0px” stylecolor=”” align=”none” link=”” linktarget=”_self” animation_type=”0″ animation_direction=”down” animation_speed=”0.1″ class=”” id=””][/imageframe]

And now let us ensure that our module is installed correctly. We can do this by going to the Admin Panel -> System -> Configuration -> Advanced -> Advanced  if all the previous steps are done properly then we can see our module in a list.

Controller:

Now we will create controller as /Insync/HelloWorld/controllers/IndexController.php

If no other controller and action are called then IndexController and IndexAction will be called by default respectively. As we can see, IndexAction will call only two methods that will  initialize and render the website page.

The next step will be adding the content to our page.

Layout configuration

All the  templates and layout files are located in the /app/design folder. Magento uses two design areas: “frontend” for the public section and “adminhtml” for the backend.

/app/design

|– /adminhtml

|– /frontend

Themes are split into various packages. By default there are two theme packages (base and default) in /app/design/frontend.

Usually the Magento theme consists of two main folders: “layout” consisting of  layout configuration files and “template” where all the “.phtml” files are stored. So we need to create layout file first as /app/design/frontend/base/default/layout/helloworld.xml

[imageframe lightbox=”yes” style_type=”none” bordercolor=”” bordersize=”0px” stylecolor=”” align=”none” link=”” linktarget=”_self” animation_type=”0″ animation_direction=”down” animation_speed=”0.1″ class=”” id=””][/imageframe]

[imageframe lightbox=”yes” style_type=”none” bordercolor=”” bordersize=”0px” stylecolor=”” align=”none” link=”” linktarget=”_self” animation_type=”0″ animation_direction=”down” animation_speed=”0.1″ class=”” id=””][/imageframe]

We have added the simple “core/template” block to our page so a separate block class is not required, as it will simply load a template file, specified in the “template” attribute.After this we will create a template file with a simple message in it

/app/design/frontend/base/default/template/helloworld/view.phtml

[imageframe lightbox=”yes” style_type=”none” bordercolor=”” bordersize=”0px” stylecolor=”” align=”none” link=”” linktarget=”_self” animation_type=”0″ animation_direction=”down” animation_speed=”0.1″ class=”” id=””][/imageframe]

[imageframe lightbox=”yes” style_type=”none” bordercolor=”” bordersize=”0px” stylecolor=”” align=”none” link=”” linktarget=”_self” animation_type=”0″ animation_direction=”down” animation_speed=”0.1″ class=”” id=””][/imageframe]

So we have set an Hello World alias, and the page will look like.

[imageframe lightbox=”yes” style_type=”none” bordercolor=”” bordersize=”0px” stylecolor=”” align=”none” link=”” linktarget=”_self” animation_type=”0″ animation_direction=”down” animation_speed=”0.1″ class=”” id=””][/imageframe]

Integrate your ERP with eCommerce stores, Marketplace and CRM