08 March 2013

Creating a PrestaShop module

A PrestaShop module consists of:
  • A root folder, named after the module, which will hold all of the module's files, and will reside in PrestaShop's /modules folder.
  • A main PHP file, named after the module, located in that root folder. This PHP file should have the same name as its root folder.
  • An icon file, named logo.gif, representing this module.
  • Optional: some .tpl files, containing the module's theme.
  • Optional: language files, if the module or its theme have text to display (and therefore, that should be translatable).
  • Optional: in a /themes/modules folder, a folder with the same name as the module, containing .tpl and language files if necessary. This last folder is essential during modifications of existing module, so that you can adapt it without having to touch its original files. Notably, it enables you to handle the module's display in various ways, according to the current theme.
Now I am going to create a module named Testmodule.

Your module can be called anything you like. My module will be called Testmoduleas that is what is will be doing - taking off line payments. So create a directory under your PrestaShop.

C:\wamp\www\prestashop\modules\Testmodule see below for an example:-
prestashop folder

Create your module's "class" .php file

PrestaShop modules must have one php file which contains the Class definition. This class definition contains the core functions of your module. The file needs to be given the same name as your module so in my case it must called testmodule.php and it needs to live in the root of the modules directory.

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. uninstall() – the same as above, except here you have to delete all your tables if necessary. Do not forget to delete configuration options too.
  
class TestModule extends SomeModule  {

 //this declares the class and specifies it will extend the some module

    private $_html = '';
    private $_postErrors = array();
 
    function __construct()
    {
        $this->name = 'testmodule';
        $this->tab = others;
        $this->version = 1;
        $this->author = 'Ankit Chauhan';

        parent::__construct(); // The parent construct is required for translations
 
        $this->page = basename(__FILE__, '.php');
        $this->displayName = $this->l('Test Module');
        $this->description = $this->l('Testing module check');
 
    }
    function install()
    {
      // your code here
    }
    function unstall()
    {
      // your code here
    }
}



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

2 comments:

  1. The blog posted is very interesting from all aspects and it will surely benefit the readers by all means.
    Hire Prestashop Developer

    ReplyDelete
  2. I am very thankful to the author to write this fruitful information.It is worth sharing for other users.Thanks once again
    Hire Prestashop Developer

    ReplyDelete