Creating Prestashop Module Part-1

0
2582
Prestashop 1.5 features for developers (Part-1 Modules)
Creating new Prestashop Hook

A basic prestashop module can be created easily. The complexity behind the module is purely associated with the features the module will be providing. A prestashop module can be built in three ways and all these depend on the requirements.

  • A simple module providing no admin interaction, just install and it will start working(Part 1)
  • A module providing In Module Configuration options (Part 2)
  • A module providing a menu link at admin control panel top menu (Part 3)

Here in part 1, we will be creating a simple prestashop module named “welcome” which will be displaying a sample text block on the left side of the page. So lets follow step by step.

1) Create a folder named “welcome” in your prestashop installation modules directory. If you dont know how to install Prestashop, kindly read this post

Prestashop Module: Folder Tree
Prestashop Module: Folder Tree

2) Now open the editor of your choice and create a php file named “welcome.php” in the welcome module folder.

Please note that the module folder name and the module main class file name should be same. Please don’t use any capital words for both files and folders name. Windows treat the capital and small names for files and folders as same, but linux consider them separate files and folders. To avoid this issue, please use small letters for both files and folders in the module. The folder and files tree will be like the attached image. The files like index.php, config.xml etc are auto generated by Prestashop, so ignore them for this tutorial.

3) The code for the module is as below. Please read the code carefully and you will understand it from the code comments also. After reading the code and the Explanation write the code to the welcome.php file.


<?php
/**
* Prestashop Module Name: welcome
* Purpose: Displays a welcome message on the left column
*/
if (!defined('_PS_VERSION_'))
exit;

class Welcome extends Module
{

  public function __construct()
  {
    /*Define properties for the prestashop module*/

    /*Prestashop Module name should be same as the module folder*/
    $this->name = 'welcome';

    /*tab, used to display the module in the modules list tabs.*/
    $this->tab = 'front_office_features';

    /* Module version */
    $this->version = '1.0';

    /* Author for the module */
    $this->author = 'Altaf Hussain';

    /* Security key used for the modul */
    $this->secure_key = Tools::encrypt($this->name);

    parent::__construct();

    /* Module display name, displayed at admin in the modules list */
    $this->displayName = $this->l('Welcome Module');

    /* Short description for the module */
    $this->description = $this->l('Displays a welcome message on left column');

  } /* End of __constructor */

  /**
   *  Installs the module and the associated database tables.
  */
  public function install()
  {
    if (!parent::install() OR !$this->registerHook('leftColumn'))
      return false;
    return true;

  } /* End of install member */

  /**
  * Uninstalls the module and delete the associated database tables
  */
  public function uninstall()
  {
    if (!parent::uninstall())
      return false;
    return true;

  } /* End of uninstall member */

  /* Hook member method to display the welcome message block on the left side
  * @params array: This array has some useful data which may be used or not. This argument is passed by prestashop by default.
  * Later on, you will find it very useful*/
  public function hookLeftColumn($params)
  {
    //Lets display our template file.
    return $this->display(__FILE__, 'welcome.tpl');

  } // End of hookLeftColumn

} /* End of welcome module calss. */

?>

Explanation: The code can be clearly understandable from the comments. Here we have two points of interest, one is the install member function at line 44 and the other is hook function at line 66. At line 40, we will need to register all the hooks where we want our module to be displayed. In our welcome prestashop module we want to display the module only on left column, so we registered it in left column only by using


$this->registerHook('leftColumn')

The registerHook function returns true if successful or false on failure.

Now after registering the hook, we will need to define its display function. We did it at line 66 by using


public function hookLeftColumn($params)
{
  //Lets display our template file.
  return $this->display(__FILE__, 'welcome.tpl');
} // End of hookLeftColumn

Some hooks may need to display some data dynamically by taking it from database so we can get and process all those data here in the hook function. Also the template file can be called here to be displayed.

4) Now where is the template welcome.tpl file? Before Prestashop 1.5.x, the template file was placed at the root of the prestashop module folder, and this is also allowed in 1.5 version for compatibility reasons. But we will follow here Prestashop 1.5.x rules. Now create a folder called “views” in the module folder, and in the views folder create another folder “templates” and in templates folder create another folder called “hook“.  Now create a file as you named in the module hook function which is welcome.tpl, as can be seen in the module folder tree in the above image. Place the below code in the welcome.tpl file.


<h2>{l s='Hello to Welcome Module' mod='welcome'}</h2>

As you may know that Prestashop is using smarty as template engine, so you may know about the smarty syntax. Smarty is beyond the scope of this tutorial, but we will come to basic smarty later on this blog.

In Prestashop templates we are placing static text as shown in template code. Starting and ending braces and then small “L”,  “l” followed by a space and then s=”Hello to welcome module” and mod=”welcome”. Note that “s” will have our static text quoted (single or double). The “mod” will have the prestashop module name. All this is used for the translation purpose. We can also place plain text like “Hello to welcome module”, but then Prestashop will be not able to translate the text. Now your prestashop module is ready to install.

5) Go to the Prestashop admin, and then click on the Modules link. Scroll down and find the “Front Office Features” link on the left side. In the list find out the module, as shown below in the picture.

Prestashop Module: Our Welcome module at Modules admin
Prestashop Module: Our Welcome module at Modules admin

6) Click on the Install button and the module will be installed. Now go to site front end and you will see the welcome message as below.

Prestashop Module: Front end display
Prestashop Module: Front end display

Our Prestashop Module is completed and you can see how easy it is to create a basic module for Prestashop 🙂 .

Prestashop 1.5 features for developers (Part-1 Modules)
Creating new Prestashop Hook

Leave a Reply