Writing your own Prestashop Module – Part 2

Creative Commons Licence

Creating a basic module

Introduction

In this second part of the series we will look at creating our first basic Prestashop module that can be controlled from the Back Office.

The Module class

Similar to the ObjectModel base class, all Prestashop modules are extended from a common base class used to define their basic functionality. The “Module” class provides the interface between the administration screens and your module as well as providing internationalisation and “hook” management functionality.

Updated for Prestashop version 1.4 onwards.

Coding conventions

Before we can write our first code there is one more element that needs to be considered — that of module class, directory and file naming.

In Prestashop, modules must be located within the /modules directory below your base store installation directory. In addition the module directory and class source file must be named in lower case, with the same name as the module class we choose. Note that the module class name itself does not have to be lowercase (by convention class names use “Camel case”), but it is essential that the folder it resides in and the source file follow this convention.

An “empty” module

In this part of the tutorial we will create a module “TutorialFirst” which demonstrates the mandatory code required to create our very first module. The first step is to create our directory on the server and name it “tutorialfirst”. We then need to create the main module class file within this directory and insert the following code:

class TutorialFirst extends Module
{
  function __construct()
  {
    $version_mask = explode('.', _PS_VERSION_, 3);
    $version_test = $version_mask[0] > 0 && $version_mask[1] > 3;
    $this->name = 'tutorialfirst';
    $this->tab = $version_test ? 'others' : 'eCartService.net Tutorials';
    if ($version_test)
      $this->author = 'eCartService.net';
    $this->version = '0.1.0';
    parent::__construct();
    $this->displayName = $this->l('First Tutorial Module');
    $this->description = $this->l('Our very first module - it does absolutely nothing at all.');
  }
}
// End of: tutorialfirst.php

We save this file as “tutorialfirst.php” in our module directory and upload to our server. Although the above code doesn’t look like very much, it is actually packed with functionality thanks to the base class it extends. Once you’ve uploaded to your development site you will be able the see the module listed under the group heading “eCartService.net Tutorials” in versions prior to Prestashop 1.4 and under the heading “Other Modules” for version 1.4 onwards. You should also now be able to install and uninstall your new module.

Let’s look at what we’re doing in this code, and why.

$version_mask = explode('.', _PS_VERSION_, 2);
$version_test = $version_mask[0] > 0 && $version_mask[1] > 3;

This code retrieves the Prestashop version information into a variable so we can check the major and minor version numbers (array elements 0 and 1 respectively). We use this to cope with changes in the module handling logic between versions. In the above code we are testing that the Prestashop version is later than 1.3.

The remaining lines of code in the constructor are concerned with setting up the basic properties of the module and ensuring that we properly inherit required functionality from the base class. First we need to set up the “internal” name of the module – note that this again follows the naming convention for the module directory and main class definition file, and again is the class name in lower case.

$this->name = 'tutorialfirst';

Next, the $this->tab member variable defines how we wish our module to be classified by the Back Office in the modules list.

$this->tab = $version_test ? 'others' : 'eCartService.net Tutorials';

In general prior to version 1.4 we would choose one the the standard categories (e.g. ‘Advertisement’, ‘Products’, ‘Tools’, ‘Blocks’ etc.) however it was entirely up to you how you wish to use this property, and in the case of these examples I’ve chosen to group them under the heading “eCartservice.net Tutorials”. From version 1.4 there are now standard values for this parameter, selected according to the following table:

tab Label
administration Administration
advertising_marketing Advertising & Marketing
analytics_stats Analytics & Stats
billing_invoicing Billing & Invoicing
checkout Checkout
content_management Content Management
export Export
front_office_features Front Office Features
i18n_localization I18n & Localization
merchandizing Merchandizing
migration_tools Migration Tools
payments_gateways Payments & Gateways
payment_security Payment Security
pricing_promotion Pricing & Promotion
quick_bulk_update Quick / Bulk update
search_filter Search & Filter
seo SEO
shipping_logistics Shipping & Logistics
slideshows Slideshows
smart_shopping Smart Shopping
market_place Market Place
social_networks Social Networks
others Other Modules

We use the $version_test variable defined above to decide which of the naming conventions to use — this maintains compatibility across the Prestashop versions. Also introduced in version 1.4 was an author property for modules, so we again use the $version_test variable to optionally define this, along with the module version number, which is consistent across versions:

if ($version_test)
  $this->author = 'eCartService.net';
$this->version = '0.1.0';

The $this->version member variable allows us to display the current module version in the modules list, which will allow users to identify visually which version of our module they are using. This may also be useful in allowing us to identify old and outdated module versions later, using an external module distribution site.

The final steps in our constructor is to call the parent class to inherit any other required initialisation, and finally set some friendly labels to use for the name and description in the back office module list. Note that these are being set using the $this->l() Module class function however, rather than just being assigned as the static strings directly.

parent::__construct();
$this->displayName = $this->l('First Tutorial Module');
$this->description = $this->l('Our very first module - it does absolutely nothing at all.');

The l() (base class) member function implements language support, and enables the store owner to provide translations. The English language version of the text we wish to display is passed to this function, and when defined in this way the store owner can use the Tools->Translations Back Office screens to translate them into the language of their choice. Wrapping any static text used in our module with this function enables the translation functionality and should be use for all static text within modules which isn’t language dependent.

Summary

Having read this article you should now be able to create a new module which can be listed and controlled from the Prestashop Back Office and optionally displaying this information in the store owner’s own language. In the next part of this tutorial we will look at extending our first module to allow configuration, and based on this configuration display output in a specified area of a page.

Sharing is caring!

Posted on July 19, 2009 | Related Categories: Tutorials | 15 comments

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

15 Responses to Writing your own Prestashop Module – Part 2

  1. Alfred says:

    I have to thanks for the endeavours you get in writing this publish. This has been an encouragement for me personally. I’ve passed this on to a friend. thankyou

  2. Make Sense says:

    line 6 does not correctly test the version version 1.3.6.0 will result in true as 3.6.0 is considered greater than 3:
    $version_test = $version_mask[0] > 0 && $version_mask[1] > 3;

    it should read

    $version_test = $version_mask[0] > 0 && $version_mask[1] >= 4;

    • Yes you’re correct, although it’s because my original testing only looked at major and minor versions and I obviously never revised it! I think the change should be to the line above:

      $version_mask = explode('.', _PS_VERSION_, 3);

      Many Thanks,

      Paul

  3. Please ignore my last comment, I was doing something stoopit….

    I had missed off the php tags at the start and end!

    Might be worth adding these in case any other idiots stumble along.

  4. Hi,

    I’m trying to work through this tutorial and have the module showing up in the back office but when I try to install it, it fails.

    As part of my trouble shooting I have copied your code exactly and have it in a file called tutorialfirst.php within a directory called tutorialfirst within /modules

    PS doesn’t give me any reason why it failed to install, it just spat the contents of the file out at the top of the screen and said:

    module not found

    with the following code:

    class TutorialFirst extends Module { function __construct() { $version_mask = explode(‘.’, _PS_VERSION_, 2); $version_test = $version_mask[0] > 0 && $version_mask[1] > 3; $this->name = ‘tutorialfirst’; $this->tab = $version_test ? ‘others’ : ‘eCartService.net Tutorials’; if ($version_test) $this->author = ‘eCartService.net’; $this->version = ’0.1.0′; parent::__construct(); $this->displayName = $this->l(‘First Tutorial Module’); $this->description = $this->l(‘Our very first module – it does absolutely nothing at all.’); } } // End of: tutorialfirst.php

    Have I done something really stoopit here?

    thanks.

    • You’ll need the following on the first line of any php files:
      <?php

      I suspect that’s missing. I had them in the code examples originally but I think somewhere down the line they got stripped out.

      • Paul,

        thanks for your speedy response, appreciated.

        You were indeed correct about the missing php tags which should have been a no-brainer considering I was naming the file *.php bit of a clue there!

        That is it all installed now and looking good.

        thanks again.

  5. Paul says:

    Hi, This looks like a good tutorial and easy to follow, which makes it all the more embarrassing that I can’t see the new module manifest in the Admin area. Using version 1.4.3.

    Many thanks,

    Paul

    • There was a slight change in 1.4 onwards to classify/categorise modules differently. I haven’t had time to update the articles to reflect this, but they should still appear under “Other Modules”.

  6. Monty Oligee says:

    ~”- I am very thankful to this topic because it really gives useful information :”*

  7. Lara Waller says:

    Love the tutorial dude, just what i was looking for.

  8. Henrik says:

    <?php

    global $_MODULE;
    $_MODULE = array();
    $_MODULE['&st;{tutorialfirst}prestashop>tutorialfirst_8c3c245232744602822902b97e65d6f9'] = 'Unser erstes Übungs-Modul';

    would be correct.

  9. Henrik says:

    hello, i’d like to know how i can create the de.php file, for the translation of the module into german for example.

    should be s.th. like

    <?php

    global $_MODULE;
    $_MODULE = array();
    $_MODULE['tutorialfirst_8c3c245232744602822902b97e65d6f9'] = ‘Erstes Übungs-Modul’;
    $_MODULE['tutorialfirst_8bd5e5a265ca16af735b925ac9d6d7f4'] = ‘Unser aller erstes Übungs-Modul – es macht überhaupt gar nichts’;

    but where do i get this _8bd5e5a265ca16af735b925ac9d6d7f4 from??!?

    thank you!

  10. @lee says:

    thanks for the article..i m looking for this exactly.

  11. Guest says:

    Thanks Alot . Keep It up coming