Thursday, February 9, 2012

Duplicate Prestashop Module


Hello developers, how is life going !?! today's tip is for prestashop developers who need to work with the same module in different place. Also it will be fine for those who want to duplicate a module and make it appear in specific pages with specific cases ( a coming soon tip ).

now let's start our steps. i will use [BlockAdvertising] module as an example.

1- Copy your module's folder, paste it with a new name. example [blockadvertising02].
2- Change the related files inside your folder to different ones. example [blockadvertising.php --> blockadvertising02.php] and [blockadvertising.tpl --> blockadvertising02.tpl] .
3- Open the file " blockadvertising02.php" , and do the following :

      3.1- Change the class name as follows:
              class BlockAdvertising02 extends Module
      3.2- Change your module's name in the class constructor as follows :
               ........
                  public function __construct()
            {
         $this->name = 'blockadvertising02';
                          ......
                          .........
                          $this->displayName = $this->l('Block advertising 2.0');
     3.3- If you don't want to share the parameters ( which is preferred ), change the parameters name as follows :
             public $adv_link02;
             ..........
             ..........
             $this->adv_link02 = htmlentities(Configuration::get('BLOCKADVERT_LINK02'), ENT_QUOTES, 'UTF-8');
              ...........
              .................
              Configuration::updateValue('BLOCKADVERT_LINK02', 'http://www.prestashop.com');
              ......................
              .............................
              if ($link = Tools::getValue('adv_link02'))
{
Configuration::updateValue('BLOCKADVERT_LINK02', $link);
$this->adv_link02 = htmlentities($link, ENT_QUOTES, 'UTF-8');
}
              ......................
              .................................
              <a href="'.$this->adv_link02.'" target="_blank" title="'.$this->l('Advertising').'">';
              ..........................
              ...................................
              <label for="adv_link02">'.$this->l('Image link').'&nbsp;&nbsp;</label><input id="adv_link02" type="text" name="adv_link02" value="'.$this->adv_link02.'" />
           
       3.4- Make your new module use your new tpl file and assign the new values for the smarty in your Hook as follows :
               $smarty->assign('adv_link02', $this->adv_link02);
return $this->display(__FILE__, 'blockadvertising02.tpl');
       3.5- (Optional ) if you want to add more hooks to make your new module transplant-able in them, for example to put your advertisement in the product footer, make as follows :
               function hookProductFooter($params)
          {
        return $this->hookRightColumn($params);
          }
4- Now open your new module's tpl file, blockadvertising02.tpl and change the following :
     <div class="advertising_block2">
<a href="{$adv_link02}" title="{l s='Advertising' mod='blockadvertising'}">...........................</a>
just to pass your new parameter "$adv_link02" to your tpl file to show the right link.

5- Final step is to open your config.xml file and change the name and display name for your new module's name that will appear in the backend as follows :
     <name>blockadvertising02</name>
<displayName><![CDATA[Block advertising 2.0]]></displayName>

AND THAT'S IT !!! .... i know it is some sort of a professional tip, but really if you follow the steps you will find it more than easy. Enjoy duplicating your modules for prestashop.

2 comments: