February 2, 2017

Magento Module Development: A Detailed Guide For Magento Developers

Categories: Blog, Development
Magento is undeniably the leading eCommerce platform today. One of the primary reasons behind this is the ever-growing community of Magento Developers. Although the Magento community edition already offers the basic features needed in setting up a functional e-commerce store, sometimes you need to create your own custom-made extensions or module.
As a Magento developer, what should you do when you need to build your own custom module in Magento? Here is a step by step guide for Magento module development.
Before you start creating a custom module in Magento, be sure you already have completed Magento installation and have turned off caching and compilation. You also need to have at least basic knowledge about the directory structure of Magento module as well as the file system.

1. Create your own Magento module xml file

Create an xml that will tell Magento that the module is active and is readily available in local codePool.

( app/etc/modules/Pw_Manageproducts.xml) <?xml version=”1.0″?>
<config>
<modules>
<Pw_Manageproducts>
<active>true</active>
<codePool>local</codePool>
</Pw_Manageproducts>
</modules>
</config>

Once you are done with this, check the Magento Admin Panel if you can be able to see your module. You can also locate the Pw_Manageproducts module in System->Configuration->Advanced->Disable Modules Output.

2. Make a directory structure for Magento module

Although you won’t need to use all of the folders listed below all the time, you will have to create them beforehand. Always be consistent in naming your folders. The most ideal format is to use capital for the first letter and then set the rest in lower case.
app/code/local/Pw/Manageproducts/
–controllers/
–sql/
–Block/
–etc/
–Helper/

3.Make a block class

Follow below code when making a block class. You can have two or more.
(app\code\local\Pw\Manageproducts\Block\Manageproducts.php)

<?php
class Pw_Manageproducts_Block_Manageproducts extends Mage_Core_Block_Template
{
}
?>

4. Prepare the front controller

You need to prepare an index controller if you need to call your Magento module through a URL. There is no need for you to create a backend module controller.

<?php
class Pw_Manageproducts_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$this->loadLayout();
$this->renderLayout();
}
}
?>
(app\code\local\Pw\Manageproducts\controllers\IndexController.php)

5. Define your config.xml file

In order to do this, just follow this:

In order to do this, just follow this:
<?xml version=”1.0” encoding=”UTF-8“?>
<config>
<modules>
<Pw_Manageproducts>
<version>1.0.1</version>
</Pw_Manageproducts>
</modules>
<global>
<models/>
<blocks>
<manageproducts>
<class>Pw_Manageproducts_Block</class>
</manageproducts>
</blocks>
</global>
<frontend>
<routers>
<manageproducts>
<use>standard</use>
<args>
<module>Pw_Manageproducts</module>
<frontName>manageproducts</frontName>
</args>
</manageproducts>
</routers>
<layout>
<updates>
<manageproducts>
<file>pw/manageproducts.xml</file>
</manageproducts>
</updates>
</layout>
</frontend>
<default>
<manageproducts>
<product>
</product>
</manageproducts>
</default>
</config>

6. Make a template file

The template file will be used to render html. Remember to use only lower case letters for folder and file name.

(app\design\frontend\default\default\template\pw\manageproducts\manageproducts.phtml)
<?php
echo<h1>We are going to manage our magento products…</h1>”;
?>

7. Make the layout.xml file

(app\design\frontend\default\default\layout\pw\manageproducts.xml)
<?xml version=”1.0“?>
<layout version=”0.1.0″>
<manageproducts_index_index>
<reference name=”root”>
<action method=”setTemplate”><template>page/1column.phtml</template></action>
</reference>
<reference name=”content”>
<block type=”manageproducts/manageproducts” name=”manageproducts” template=”pw/manageproducts/manageproducts.phtml”/>
</reference>
</manageproducts_index_index>
</layout>

You now have a new Magento module. But, if you are still not sure how to use the plugin, you may try one of these simple ways to use it.

1. Use it in a PHP file.

To do this, see below.

<?php
echo $this->getLayout()->createBlock(‘manageproducts/manageproducts‘)
->setBlockId(‘manageproducts‘)->toHtml() ; ?>

2. Use it in CMS Content Editor.

If you choose to use this way, you do not have to make the layout.xml file.
{{block type=”manageproducts/manageproducts” template=”pw/manageproducts/manageproducts.phtml”}}

3. Through a URL

www.example.com/index.php/manageproducts You just have to replace the frontName(manageproducts) with the one you defined in the config.xml file.
admin