Child pages
  • Creating a PrestaShop module

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
public function install()
  {
  return ( parent::install() AND $this->registerHook( 'leftColumn' ) );
  }

This code checks:

  • the boolean value returned by the Module class' install() method: if true, the module is installed and can be used.
  • the boolean value returned by registerHook() for the 'leftColumn' hook: if true, the module is indeed registered to the hook it needs, and can be used.

If any of these two boolean values is false, then install() returns

As it is, In this first and extremely simplistic incarnation, this method is useless, since all it does is check the value returned by the Module class' install() method. Moreover, if we hadn't created that method, the superclass' method would have been called instead anyway, making the end result identical.
Nevertheless, we must mention this method, because it will be very useful once we have to perform checks and actions during the module's installation process: creating SQL tables, copying files, creation configuration variables, etc.

Likewise, the module should contain an uninstall() method, so as to have a custom uninstallation process. This method could look as such:

Code Block
public function uninstall()
  {
  if ( !parent::uninstall() )
    Db::getInstance()->Execute( 'DELETE FROM `' . _DB_PREFIX_ . 'block_cms` WHERE `id_block` = ' . intval( $this->id ) )mymodule`' );
  parent::uninstall();
  }

To put the finishing touch to this basic module, we can add an icon, which will be displayed next to the module's name in the back-office modules list.
The icon file must respect these requirements:

  • 16*16 image.
  • named logo.gif.
  • place placed on the module's main folder.

You can find an excellent free set of free icons to pick from on the FamFamFam website.

Now that all basics are in place, put the module's folder in the /modules folder of your PrestaShop test install, open PrestaShop, and in the "Modules" tab, under "Other Modules", you should find your module. Install it in order to be able to manager manage it for the rest of this guide.

...

PrestaShop automatically creates a small config.xml file in the module's folder, which stores a few configuration information. You should NEVER edit it by hand.

Hooking a module

Displaying data, starting a process at a specific time: in order for a module to be "attached" to a location on the front-office or the back-office, you need to give it access to one of the above cited hooksmany PrestaShop hooks, described earlier in this guide.

To that effect, we are going to change your module's code, and add these lines:

...

Code Block
if ( parent::install() == false OR !$this->registerHook( 'leftColumn' ) )
  return false;
return true;

We changed the original line to add a second test. Now this line

This code checks:

  • the boolean value returned by the Module class' install() method: if true, the module is installed and can be used.
  • the boolean value returned by registerHook() for the leftColumn hook: if true, the module is indeed registered to the hook it needs, and can be used.

If any of these two boolean values is false, install() returns false too, and the module cannot be installed. Both values have to be true for the module to be considered installed.

Therefore, this line now reads this way: if installation or hooking fail, we inform PrestaShop.

...

Smarty is a PHP template engine, and is used by PrestaShop's theming system.

It parses TPL files, look looking for dynamic elements to replace by their data equivalents, then displays the generated result. Those dynamic elements are indicated with curly brackets ?. The coder : { ... }. The programmer may create new variables and use them in TPL files.

...

Code Block
{* This string is commented out *}

{*
This string is too!
*}

Unlike with a HTML commentcomments, commented-out Smarty code is not present in the final output file.

...

In this section you will learn how to give your module its own tab or sub-tab, in a matter of minutes.

Follw Follow these steps:

  1. Add a new table to your PrestaShop database, named ps_test. Give it two fields:
    • id_test (INT 11)
    and
    • test (VARCHAR 32)
    .
  2. Create a blank file named Test.php in PrestaShop's /classes folder.
  3. Add the following lines to that file:

...