Child pages
  • Creating a PrestaShop module

Versions Compared

Key

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

...

Code Block
titlemymodule.php
borderStylesolid
<?php
class MyModule extends Module
  {
  public function __construct()
    {
    $this->name = 'mymodule';
    $this->tab = 'Test';
    $this->version = 1.0;

    parent::__construct();

    $this->displayName = $this->l( 'My module' );
    $this->description = $this->l( 'Description of my module.' );
    }

  public function install()
    {
    if ( parent::install() == false )
      return false;
    return true;
    }
  }
?>

...

Code Block
$this->displayName = $this->l( 'My module' );

Assigning a public name for the module, which will be displayed in the back-office's modules list.
The l() method is part of PrestaShop translation's tools, and is explained further below.

...

Code Block
public function install()
  {
  if ( parent::install() == false )
    return false;
  return true;
  }

...

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

...

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 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.

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 hooks.

...

Code Block
titlemymodule.php (partial)
borderStylesolid
public function install()
  {
  if ( parent::install() == false OR !$this->registerHook( 'leftColumn' ) )
    return false;
  return true;
  }

...

public function hookLeftColumn( $params )
  {
  global $smarty;
  return $this->display( __FILE__, 'mymodule.tpl' );
  }

public function hookRightColumn( $params )
  {
  return $this->hookLeftColumn( $params );
  }

Let's explore these new or changed lines.

...

In the transplantation form, find "My module" in the modules drop-down menu, then choose "Left column blocks" in the "Hook into" drop-down menu. Save.

Warning

It is useless to try to attach a module to a hook for which it has no implemented method.

Save. The "Positions" page should reload, with the following message: "Module transplanted successfully to hook". Congratulations! Scroll down, and you should indeed see your module among the other modules from the "Left column blocks" list. Move it to the top of the list.

Image Added

Displaying content

Now that we have access to the left column, we should display something there.

As said earlier, the content to be displayed should be stored in .tpl files. We will create the mymodule.tpl file, which was passed as a parameter of the display() method in our module's code.

Let's create the mymodule.tpl file, and add some lines of code to it.

Code Block
titlemymodule.tpl
borderStylesolid

<!-- Block mymodule --> 
<div id="mymodule_block_left" class="block">
  <h4>Welcome!</h4>
  <div class="block_content">
    <ul>
      <li><a href="{$base_dir}modules/mymodule/mymodule_page.php" title="Click this link">Click me!</a></li>
    </ul>
  </div>
</div>
<!-- /Block mymodule -->

Save the file in the module's root folder, reload your shop's homepage: it should appear on top on of the left column, right below the shop's logo.

Image Added

The displayed link doesn't lead anywhere. If you need to test it, add the needed mymodule_page.php file in the module's folder, with a minimal content, such as "Welcome to my shop!" The resulting page will be very raw, so let's see if we can use the theme's style instead.

As you would expect, we have to create a TPL file in order to use the theme's style. Let's create the mymodule_page.tpl file, which will contain the basic line, and call that file from mymodule_page.php, which will add the theme's header, footer, etc.

Code Block
titlemymodule_page.tpl
borderStylesolid

Welcome to my shop!
Code Block
titlemymodule_page.php
borderStylesolid

<?php 
global $smarty; 
include( '../../config/config.inc.php' );
include( '../../header.php' );

$smarty->display( dirname(__FILE__) . '/mymodule_page.tpl'); 

include( '../../footer.php' );
?>
Tip

You should strive to use explicit and recognizable names for your TPL files, so that you can find them quickly in the back-office – which is a must when using the translation tool.