Child pages
  • Creating a PrestaShop module

Versions Compared

Key

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

...

Let's create a simple first module; this will enable us to better describe its structure. We 'll will call it "My module".

First, create the module folder. It should have the same name as the module, with no space, only alphanumerical characters, the hyphen and the underscore, all in lowercase: /mymodule.

...

The front-office part of the module is defined in a .tpl file placed in a specific folder: /views/templates/front/. TPL files can have just about any name. It there 's is only one such file, it is good practice to give it the same name as the folder and main file: mymodule.tpl.

...

This checks for the existence of a PHP constant, and if it doesn't does not exist, it quits. The sole purpose of this is to prevent visitors to load this file directly.

...

In this first and extremely simplistic incarnation, this method does the minimum, since all it does is return the value returned by the Module class' install() method, which returns either true if the module is install, or false otherwise. Moreover, if we hadn't had not 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.

...

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

// then...

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

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

...

The link that the module displays doesn't does not lead anywhere for now. 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.

...

Code Block
titleAdminTest.php
borderStylesolid
<?php
include_once( PS_ADMIN_DIR.'/../classes/AdminTab.php');

class AdminTest extends AdminTab
{
  public function __construct()
  {
    $this->table = 'test';
    $this->className = 'Test';
    $this->lang = false;
    $this->edit = true;
    $this->delete = true;
    $this->fieldsDisplay = array(
      'id_test' => array(
        'title' => $this->l('ID'),
        'align' => 'center',
        'width' => 25),
      'test' => array(
        'title' => $this->l('Name'),
        'width' => 200)
    );

    $this->identifier = 'id_test';

    parent::__construct();
  }

  public function displayForm()
  {
    global $currentIndex;

    $defaultLanguage = intval(Configuration::get('PS_LANG_DEFAULT'));
    $languages = Language::getLanguages();
    $obj = $this->loadObject(true);

    echo '
      <script type="text/javascript">
        id_language = Number('.$defaultLanguage.');
      </script>';

    echo '
      <form action="' . $currentIndex . '&submitAdd' . $this->table . '=1&token=' . $this->token . '" method="post" class="width3">
        '.( $obj->id ? '<input type="hidden" name="id_'.$this->table.'" value="'.$obj->id.'" />' : '' ).'
        <fieldset><legend><img src="../img/admin/profiles.png" />'.$this->l('Profiles').'</legend>
          <label>'.$this->l('Name:').'</label>
          <div class="margin-form">';

    foreach ($languages as $language)
      echo '
          <div id="name_'.$language['id_lang'|'id_lang'].'" style="display: '.($language['id_lang'|'id_lang'] == $defaultLanguage ? 'block' : 'none').'; float: left;">
            <input size="33" type="text" name="name_'.$language['id_lang'|'id_lang'].'" value="'.htmlentities($this->getFieldValue($obj, 'name', intval($language['id_lang'|'id_lang'])), ENT_COMPAT, 'UTF-8').'" /><sup>*</sup>
          </div>';

    $this->displayFlags($languages, $defaultLanguage, 'name', 'name' );
    echo '
          <div class="clear"></div>
        </div>
        <div class="margin-form">
          <input type="submit" value="'.$this->l('Save').'" name="submitAdd'.$this->table.'" class="button" />
        </div>
        <div class="small"><sup>*</sup> '.$this->l('Required field').'</div>
      </fieldset>
    </form> ';
  }
}
?>

Put the files online, then create the tab by going to the "Employee" tab, then its "Tabs" sub-tab. Click the "Add new" button, and fill-in the fields with the class' name, "AdminTest". Do not confuse "class" with "modules"! Choose an icon (like one from the FamFamFam icon pack: http://www.famfamfam.com/lab/icons/silk/), choose where the tab should go, and save. You 're are set! Now start customizing it to your needs!

...