How to Create a Magento Custom Module?

  • Cubettech
  • Web App Development
  • 9 years ago

Magento is simple and powerful!
Creating a custom module in Magento is the important development process for any Magento developer. As you know, at some point of time, a developer might want to integrate his/her own functionality/module. With this blog, I’m trying to throw light on how to develop a Custom Module in Magento.

I assume, you have a basic knowledge about Magento and its structure. As you would know, Magento consists of 3 type of code pool (Code, Core and Local)where custom and core modules reside. The local pool(folder) is where all the custom modules reside or should be placed.

Now moving on to the creation of a module. Below given are the components (folder structure) that a module should contain.

Blocks – Here you need to provide the functions used to display data in our templates.
Models – All business logic are contained here.
Resource – Models contains functions that are used for database interaction.
Controllers – Defines page layout and blocks files and are loaded when a URL is requested.
etc – Contains configuration files in XML formats which tells Magento how many files modules have and how the module interacts.
Helpers – Contains functions that are used for defining common business logic (such as image resize, validation). These functions can be used anywhere across the Magento application
sql – Contains SQL scripts to create, modify, or delete SQL tables.

Now create the directories as shown:

Now, By creating config file we shall configure our module.Create a config file, Cubet_Mymodule.xml in app/etc/modules directory. This modules directory consists of config files for all modules.

true local

This is the file which communicates with magento about the location of our custom plugin. As you can see in the above code we have provided a tag ‘<active>’ and have specified ‘true’ to enable our module. Now, if you check our Magento admin side ->System->Configuration->Advanced->Disabled Modules all the list of modules can be seen here.

So far we just activated our module, now, we need to move to the functional or the development part of our plugin. For the cause, move onto app/code/local/Cubet/Mymodule/etc and create a config.xml file.

0.1.0 standard Cubet_Mymodule mymodule


1. The ‘module’ tag contains the name and version of our module.
2. The ‘frontend’ tag tells Magento about controller dispatched.
3. ‘routers’ explains accessing our module using routing mechanism.
4. And the module name is defined inside ‘mymodule’ tag
5. ‘frontend’ tag is for defining frontend name. Using this frontend name we can access our custom module in frontend.

When calling or Magento will look for index of your module’s controller file. Hence, we need to create our controller file.

For that move to app/code/local/Cubet/Mymodule/controllers and create file IndexController.php with following content.

P.S. Each filename and classname are case sensitive. So, name of the class should be on the basis of name you provided in router.

Now if you check the site with the url it will print “Welcome to my custom magento module”.

Understanding the above provided code:

As you can see the class extended Mage_Core_Controller_Front_Action that contain all methods which are used for the routing URL and the class name helps in finding the location of class name. That is this class reside in Mage-> Core->Controller->Front-> Action.php

So, the classname of our controller is Cubet_Mymodule_IndexController and you should make sure that controller is defined in <module>tag_ActionControllername_Keyword Controller.

– Name of our module Cubet_Mymodule
Action Controllername – Index
Action Controller – along with the keyword

As per the above mentioned pattern, our controller name is:

That’s it! And now you have developed a custom module and implemented the controlling dispatch.

Here is few commonly used function
Below I have mentioned few functions used in Magento. There are many ways to approach on generating the URL

1. getURL (getBaseUrl, getSkinUrl)
2. By using helper/model class

#) To get store URL

$url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);

#) To generate current page Url

$url = Mage::helper('core/url')->getCurrentUrl();

#) To generate url dynamically

$url = $this->getUrl('page.html');

#) To load the css file/Js/Image file residing in skin directory

$url = $this->getSkinUrl('images/file.jpg');

Product/Category related functions

#) To load current category while you are on a category page

$_category = Mage::getModel('catalog/layer')->getCurrentCategory();


$_current_category = Mage::registry('current_category');

#) Referencing category through the ID

$_category = Mage::getModel('catalog/category')->load($category_id);

It is possible to access a product with ID within a category page

$_product = Mage::getModel('catalog/product')->load($product_id);

#) Other than product ID there is another unique identifier in magento for products, its SKU.

$_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $product_sku);

Checkout/ Cart related function

#) To get the count of total

$_cartQty = $this->getSummaryCount()? $this->getSummaryCount() : 0

#) To loop through the items added recently to the cart

getRecentItems() ?> getItemHtml($_item) ?> __('You have no items in your shopping cart.') ?>

#) Display the subtotal of the items added in the cart

formatPrice($this->getSubtotal()) ?>

#) To generate dynamic link to checkout & Cart

getCheckoutUrl() ?> // Generates Checkout page URL getUrl('checkout/cart'); ?> // Generates Cart page URL

There are many more functions provided in Magento. I hope using the usage of these functions would help you to surpass the basic hurdles with ease.

Know More About This Topic from our Techies

Table of Contents

    Contact Us


    What's on your mind? Tell us what you're looking for and we'll connect you to the right people.

    Let's discuss your project.