How to create PrestaShop module?

PrestaShop is a quite complicated e-commerce system, however you can do almost anything with custom made modules and I will introduce you to the process of creation them.

Modules on PrestaShop are specific PHP classes that are based on systems hooks (which you can easily create after reading my tutorial or use PrestaShop default ones). This makes all the necessary changes less complicated and the integration becomes a lot easier with the core and frontend.

For example, there are different hooks which are executed on making orders, all the sidebars and menus and etc. You can find all hooks list on your PrestaShop database in ‘ps_hook’ table (if your prefix is ps_) and I have not found other way to really be sure which hooks are available for you because many modules tend to create some custom hooks.

Moreover, you will find descriptions about every hook that was created when installed.

We will use two default hooks – LeftColumn, RightColumn. These hooks are for left and right sidebars and they work as widgets in WordPress, except you create and add them by yourself.

The first thing you need to do is to create a folder with your module name. If you opened PrestaShop modules catalog, you would see that there is a convenient way to call them. For example, all the sidebar blocks (think of them as WordPress widgets) are with the prefix block and so on.

So as we are also creating a block we should add this prefix to our name. Let’s call our plugin blockFirstModule.

You should name your PHP class the same as your catalog for consistency and in order to avoid system errors. Mind capitalized letters.

Every PrestaShop module has to consist of three necessary functions:

  1. __construct() – here you provide all the information about your module – module name, version and description which are visible in the backend. In addition, you can choose in which tab to show your module, for example, Products.
  2. install() – this function also does not have parameters, but is very important. Here you have to register all the hooks you will be using, create necessary database table changes (create, add columns, prepare it and so on) and initiate default module options.
  3. uninstal() – the same as above, except here you have to delete all your tables if necessary. Do not forget to delete configuration options too.

So for now we have this part of code:

class blockfirstmodule extends Module {

	function __construct()
	{
        $this->name = "blockfirstmodule";
        $this->tab = 'Tab Name';
        $this->version = '0.1.0';
        parent::__construct();
        $this->displayName = $this->l('Our First PS Module');
        $this->description = $this->l('We have created a simple PS Module.');
	}

	function install()
	{

	}

	function uninstal()
	{

	}

}

__construct() content is self explaining.

Now we have to register our hooks. The best way to do it is to create single if statement which will indicate if the module was created successfully or not, so if there were some problems, for example, with registering hooks or creating database table, we will have an error on the backend and confirmation otherwise.

This code displays everything in practice.

	function install()
	{
       if (!parent::install()
           OR !$this->registerHook('rightColumn')
           OR !$this->registerHook('leftColumn'))
                return false;
        return true;
	}

We need to do the same with uninstall(). Here is code:

	function uninstal()
	{
		if (!parent::uninstall())
			return false;
		return true;
	}

We need to call special methods to activate our registered hooks and give them information what to do. All methods representing hooks have to start with hook prefix, for example, hookLeftColumn() and have one parameter called $params.

This time we will only echo some text to the sidebars, so here is an example, how to work with PrestaShop hooks:

	public function hookLeftColumn($params)
	{
		echo "Hello World!";
	}

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

As you can see we mirror the functionality of left column hook with the right one. So there’s no difference if you choose to add this module to the right or left sidebar of your PrestaShop template.

And here you have your own made module for e-commerce system.

Now you can upload everything to your server and install module from the backend.

Download Full Code Example.

Leave a comment

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. Cookies are only used for statistical purposes.

Close