>_How to make Prestashop Modules?

Posted 13 years ago  118751 reads  

Author: Marghoob Suleman

I was also thinking like you. How to make prestashop modules? Where to start?
Ooops... I forgot to say Hi! How are you doing my friend? Smile :)

Back to topic; Believe me its really simple to create a prestashop module. All you need to understand how prestashop modules works. I've attached a sample module at the end of this article.

Well... All modules are attached with hooks. What is hook? Hook is actually a module holder. By default prestashop have some hooks i.e. rightColumn, leftColumn, payment, paymentReturn, updateOrderStatus, top, footer, productActions, cart, customerAccount, header, productTab, productTabContent, home and lots more. I'll write about hooks later.

for creating new hook you may use this tutorial, http://www.prestashop.com/forums/viewthread/12637/ however I've not tried that but it should work.

Okay come to the point.

I am creating a module called "fashion" for you to understand a prestashop module work flow. This module will display an image.

Front Office design

  1. Create a folder fashion inside modules.
  2. Create a page fashion.tpl for displaying your image in front office. You can use your favorite editor like editplus, notepad etc. I use Dreamweaver. I love Adobe products :)
  3. Write following code in your fashion.tpl file.
    
    <div class="block">
    	<h4>Fashion</h4>
    	<div class="block_content" style="padding:0px;">
    	My First Prestashop module
    	<a href="http://www.giflelo.com/">
        <img src="{$module_dir}fashion.jpg" border="0" />
        </a>
    	</div>
    </div>
    
    
  4. {$module_dir} is prestashop variable; returns your current module path.

You are done with "Front Office" design.

Back Office

  1. Create a file fashion.php
  2. Copy and paste the following code. we'll discuss about functions after this peace of code.
    
    <?php
    class Fashion extends Module
    {
    	function __construct()
    	{
    		$this->name = 'fashion';
    		$this->tab = 'Blocks';
    		$this->version = 1.0;
    
    		parent::__construct(); // The parent construct is required for translations
    
    		$this->page = basename(__FILE__, '.php');
    		$this->displayName = $this->l('Block Fashion');
    		$this->description = $this->l('Add a fashion block');
    	}
    
    	function install()
    	{
    		if (!parent::install())
    			return false;
    		if (!$this->registerHook('leftColumn'))
    			return false;
    		return true;
    	}
    
    	/**
    	* Returns module content
    	*
    	* @param array $params Parameters
    	* @return string Content
    	*/
    	function hookLeftColumn($params)
    	{
    		return $this->display(__FILE__, 'fashion.tpl');
    	}
    
    }
    ?>
    
    
  3. Lets discuss

    Let's talk about __construct(), is invoked automatically when a class is called. I've added my comments below.

    
    function __construct()
    	{
    		$this->name = 'fashion'; // this is your module id/name
    		$this->tab = 'Blocks'; // tab is actually a hook holder
    		$this->version = 1.0; // and here is your program version
    
    		parent::__construct(); // The parent construct is required for translations
    		$this->page = basename(__FILE__, '.php');//please note your folder name and module name should match.
    		$this->displayName = $this->l('Block Fashion'); // display module name in admin panel modules list
    		$this->description = $this->l('Add a fashion block'); // and this is your modules description
    	}
    
    

    And what about install() function? Well this is called by prestashop framework. It checks that your module is installed or not, you can modify hook names here. Please note that you have to write a function respective to your hooks defined and you are allowed to add multiple hooks :).

    
    function install()
    	{
    		if (!parent::install())
    			return false;
    		if (!$this->registerHook('leftColumn'))
    			return false;
    		return true;
    	}
    
    

    This is last step. We've defined registerHook('leftColumn'), so we've written following function. This method is called by front office to check your content path/variable/smarty etc.

    
    function hookLeftColumn($params)
    	{
    		return $this->display(__FILE__, 'fashion.tpl'); // this is your .tpl file. You can add css/javascript links in your tpl file.
    	}
    
    

Thank you for your time. Please feel free to ask any question and feedback.

Powered by HashtagCms.org