Book Image

PrestaShop Module Development

By : Fabien Serny
Book Image

PrestaShop Module Development

By: Fabien Serny

Overview of this book

Table of Contents (19 chapters)
PrestaShop Module Development
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

First steps


First of all, we have to choose a public and technical name for the module. The technical name must be in lowercase, contain only letters and numbers, and begin with a letter. Let's call the module publicly My Module of product comments and technically mymodcomments.

You can choose whatever names you want as long as you stick to them for the rest of the book. However, since the technical name will be the directory name of your module, you won't be able to have two modules with the same technical name in a PrestaShop. Moreover, your technical name has to be unique if you want to sell it on marketplaces. One trick is to prefix it with your company name (in my case, my company name is Froggy Commerce, so I prefixed all my modules with froggy).

On the other hand, the public name has no restriction, so you can write whatever you want for the merchant.

Now, we will begin to create our first module.

Open the modules directory in the root of the PrestaShop directory, then create a new directory and name it with the technical name we chose: mymodcomments. Once done, in this new directory, create a new blank PHP file and name it with the technical name as well (in our case, mymodcomments.php). This is the main file of the module. The following screenshot depicts the file and folder structure:

Now open the mymodcomments.php file, and write the class of your module. You must name it with your technical name. To make it easier to read, you can use CamelCase. The class must extend PrestaShop's Module class. This class contains all the needed methods to make a module work; without it, your module won't work. In our case, the class of the module will be:

<?php
class MyModComments extends Module
{
}

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Additionally, the code for this book is part of a Git repository, which is available on GitHub at https://github.com/FabienSerny/mymodcomments, https://github.com/FabienSerny/mymodcarrier, and https://github.com/FabienSerny/mymodpayment.

In order to have a working module, we just have to add the __construct method. In this method, you must write the following three mandatory lines:

  • Set the technical name: Without this, the module won't be installable. This variable is used by PrestaShop to build the install, uninstall, and configure links of the module:

    $this->name = 'mymodcomments';
  • Set the public name: This line is used to display the module name for the merchant in the modules list of the back office:

    $this->displayName = 'My Module of product comments';
  • Calling the parent __construct method: This line is mandatory since important initializations are made in this function:

    parent::__construct();

You can add some of the following optional lines to give information about the module:

  • Set the module category: This makes the module search easier. If you don't set it or set a wrong value, then the module will automatically be associated with the Other Modules category. You should set one of the values shown in the following table:

    administration

    advertising_marketing

    analytics_stats

    billing_invoicing

    Checkout

    content_management

    export

    emailing

    front_office_features

    i18n

    localization

    merchandizing

    migration_tools

    payments_gateways

    payment_security

    pricing_promotion

    quick_bulk_update

    search_filter

    seo

    shipping_logistics

    slideshows

    smart_shopping

    market_place

    social_networks

    others

    mobile

      

    These possible values are associated with the module's categories search filter in the back office of your PrestaShop:

    $this->tab = 'front_office_features';
  • Set the module version: This variable will not only be used to display the module version in the module's list of the back office but also to check if updates are available (we'll cover this in more detail later):

    $this->version = '0.1';
  • Set the author name: This line is used to display the author name for the merchant in the module's list of the back office. It can also be used to search for modules:

    $this->author = 'Fabien Serny';
  • Set the module description: This helps the merchant learn what the module is used for:

    $this->description = 'With this module, your customers will be able to grade and comments your products';

Here's what your code should look like now:

<?php
class MyModComments extends Module
{
  public function __construct()
  {
    $this->name = 'mymodcomments';
    $this->tab = 'front_office_features';
    $this->version = '0.1';
    $this->author = 'Fabien Serny';
    $this->displayName = 'My Module of product comments';
    $this->description = 'With this module, your customers will be able to grade and comments your products.';
    parent::__construct();
  }
}

At this point, you should be able to see your module in the back office of the modules section, as shown in the following screenshot:

Note

The question mark icon

This is the default logo for all modules. If you want a custom picture, you just have to add a logo.png picture of 32 x 32 pixels and a logo.gif picture of 16 x 16 pixels (for PrestaShop 1.4 compliancy) at the root of your module directory.

The module is now a working module; you can install it by clicking on the Install button. Here is what you should see:

For the moment, your module does nothing and doesn't have any configuration options. The only actions available are:

  • Uninstall: This option uninstalls the module and deletes the specific configurations, if there are some.

  • Disable: This is an alternative to the uninstall action but permits you to keep module configurations. The module is still installed and will simply be ignored by PrestaShop.

  • Reset: This option uninstalls and reinstalls the module.

  • Delete: This option will uninstall the module and then delete all the module files.

All these core functions are handled by the Module class and can be overridden by the module (we will see this later).