Versions Compared

Key

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

...

Once you’ve selected a name, create a directory in the /modules directory with the name you selected.  Next, create a file in the new directory with the same name as the directory followed by .php.  For example, if the name of your module is helloworldnc, then create a file helloworldnc.php in the helloworldnc directory.  In that file, copy the required if statement from the top of another module, then add a new class with the same name as the file that extends the Module class.  The case of the class name isn’t important, though it is convention to use an uppercase letter at the start of each word.  Here’s an example:

...

Code Block
public function __construct()
{
    $this->name = 'helloworldnc';
    $this->tab = 'front_office_features';
    $this->version = '1.0';
    $this->author = 'Nethercott Constructions';
    parent::__construct();
    $this->displayName = $this->l('Hello World');
    $this->description = $this->l('This is an example module');
    $this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
    if (!Configuration::get('HELLO_WORLD_NC_NAME'))
        $this->warning = $this->l('No name provided');
}

The name variable must be the directory name you chose for the module.  The tab variable is used to select which section the module appears in.  It must be one of the following values:

...

Any value other than the ones above will place the module in the Other Modules section.

The version variable can be any number you like.  It is used to make it easy to identify which version of the module you are using.  It is convention to use numbers below 1.0 before all the planned features have been added to the module, then increment the value as bugs are fixed and more features are added.

The author variable is the author name displayed after “by” in the module listings.  It can also be used to filter modules by author.

Next, the parent constructor is called, which sets the name variable to the module’s ID if no name was provided, then it adds the module to the cache, then creates a _path variable with the module’s path.

The displayName variable must be the module name that is displayed in bold on the Modules tab and the description variable must be the description of the module displayed below the module’s name.

The confirmUninstall variable is an optional variable that lets you specify a confirmation message to display before uninstalling the module.  If uninstalling your module will cause important information to be lost, then it is a good idea to add a confirmation message.

...

You must also put a 16 x 16 pixel GIF image called logo.gif in your module’s directory.  Your module should then display on the Modules tab.

...

The next thing you should do is add a config.xml file to your module.  Although one isn’t required, it helps to reduce the amount of memory required by the Modules tab by loading only the file instead of the entire module to get the module’s name and description.  Here is an example config.xml file:

Code Block
<?xml version="1.0" encoding="UTF-8" ?>
<module>
    <name><![CDATA[helloworldnc]]></name>
    <displayName><![CDATA[Hello World]]></displayName>
    <version><![CDATA[1.0]]></version>
    <author><![CDATA[Nethercott Constructions]]></author>
    <description><![CDATA[This is an example module]]></description>
    <tab><![CDATA[front_office_features]]></tab>
    <is_configurable>1</is_configurable>
    <need_instance>1</need_instance>
    <limited_countries></limited_countries>
</module>

The first line is used to define the file as an XML file with UTF-8 encoding.  The rest of the file creates a module object with a number of variables.  The values of most variables are wrapped in <![CDATA[ ]]> to prevent the values being parsed as XML.  The name variable is the directory name of the module.  It is important for this value to match the module’s directory, otherwise the logo will not display and none of the module links will work. 

The displayName, version, author, description and tab variables should be the same as the module display name, version, description and tab that you specified in the module’s code.  It is best that they match the ones in the module, though no errors will occur if they don’t.

The is_configurable variable is used to indicate whether the module has a configuration page.  If it does, set the variable to 1, otherwise set it to 0.  It is important for this value to be correct, otherwise the Configure link may be missing from the module, making it impossible to access the configuration page, or cause an error when it is clicked and no configuration page actually exists.

The need_instance variable is used to indicate whether an instance of the module needs to be created when the Modules tab is loaded.  If the value is 0, then the module will not be loaded, which will save time and memory.  If the value is 1, then the module will be loaded.  If your module may need to display a warning message on the Modules tab, then you should choose the value 1.  Otherwise, choose 0 to save time and memory.

The limited_countries variable can optionally be used if a module should only be available in a specific country. For example, the following line uses the country ISO code to limit a module to France only:

Code Block
<limited_countries>fr</limited_countries>

Adding the Install and Uninstall Functions

Although your module is now displayed on the Modules tab, you won’t be able to install it yet.  You will need to add an install() function inside the class before you can install the module.  Here’s an example install() function:

Code Block
public function install()
{
    if (!parent::install() OR
        !$this->registerHook('leftColumn') OR
        !$this->registerHook('header') OR
        !Configuration::updateValue('HELLO_WORLD_NC_SHOW_NAME', 1) OR
        !Configuration::updateValue('HELLO_WORLD_NC_NAME', 'World'))
        return false;
    return true;
}

The install() function uses an if statement to call the parent install function, place the module in left column and header hooks, and set variables to their default values.  If any of these return false, then the install() function will return false, which causes PrestaShop to display an error message.

If they all return true, then the install() function will return true, which causes PrestaShop to display the "Module installed successfully" message.

It is also a good idea to add an uninstall() function, so that all the module’s settings are removed from the database when the module is not being used.  For example:

Code Block
public function uninstall()
{
    if (!parent::uninstall() OR
        !Configuration::deleteByName('HELLO_WORLD_NC_SHOW_NAME') OR
        !Configuration::deleteByName('HELLO_WORLD_NC_NAME'))
        return false;
    return true;
}

The uninstall() function uses an if statement to call the parent uninstall() and delete all configuration values related to the module from the database.  Like the install function, the uninstall() function returns false if any of these return false or returns true otherwise, which causes messages similar to the ones above to be displayed.

...