Jump to content

[GUIDE] PrestaShop Development Guide


rocky

Recommended Posts

Thanks everyone. I'll try to create a PDF version of the manual when I have time. The "Module Hooks" section is incomplete at the moment. I'll try to update it too.

EDIT: I've now finished the "Module Hooks" section and added a "Cookie Structure" section too.

  • Like 2
Link to comment
Share on other sites

Sorry but for overriding module:


Overriding Modules

PrestaShop allows you to override a module's template files inside a theme, so that different themes can display modules differently. You cannot override a module's PHP files though, so you must use the same module code for all themes. It is just the view that can be changed. To override a module's template file, mirror the file's path inside the theme. For example, to make a Web 2.0-style footer, copy modules/blockcms/blockcms.tpl to themes//modules/blockcms/blockcms.tpl, then edit the footer code at the bottom. Your theme will use the new code, while the default PrestaShop theme will continue to use the original PrestaShop code.


I don't understand this line:
then edit the footer code at the bottom.


What i'm supposed to edit ?
And if a module contains more than one .tpl, what should I copy ? edit ?

Regards,

Nadine
Link to comment
Share on other sites

It's the code after the {else} you should edit:

>
{else}
   <!-- MODULE Block footer -->
</pre>
<ul>
       {if !$PS_CATALOG_MODE}getPageLink('prices-drop.php')}" title="{l s='Specials' mod='blockcms'}">{l s='Specials' mod='blockcms'}{/if}
getPageLink('new-products.php')}" title="{l s='New products' mod='blockcms'}">{l s='New products' mod='blockcms'}
       {if !$PS_CATALOG_MODE}getPageLink('best-sales.php')}" title="{l s='Top sellers' mod='blockcms'}">{l s='Top sellers' mod='blockcms'}{/if}
       {if $display_stores_footer}getPageLink('stores.php')}" title="{l s='Our stores' mod='blockcms'}">{l s='Our stores' mod='blockcms'}{/if}
getPageLink('contact-form.php', true)}" title="{l s='Contact us' mod='blockcms'}">{l s='Contact us' mod='blockcms'}
       {foreach from=$cmslinks item=cmslink}
           {if $cmslink.meta_title != ''}
{$cmslink.meta_title|escape:'htmlall':'UTF-8'}
           {/if}
       {/foreach}
{l s='Powered by' mod='blockcms'} PrestaShop™
</ul>
<br>   <!-- /MODULE Block footer --><br>{/if



From here, you can add or remove links, or add multiple floated

  • s to create a web 2.0-style footer.
  • Like 1
Link to comment
Share on other sites

And if the module has many .tpl ? What we must put on the folder theme/mytheme/modules/ ? all .tpl with all edition of them ?

Second point: if I have 30 modules in my main theme and only 25 in my theme named "mytheme", only the 25 will be running if I activate "mytheme" ? Because all (30) are activated in the BO...

Third point: if in my module folder named "toto" and I have titi.tpl, in the toto folder it is wrong ?
Example with bankwire: the folder is "bankwire" and inside I have three .TPL: payment.tpl, paymentexecution.tpl and paymentreturn.tpl

Thx for answer of this three point !

And for all module, what is the line to be modified ? Link to .php ? link to .css ? both ?

Link to comment
Share on other sites

There is no need to copy over all module TPL files. You should just copy over just the TPL files you want to override. If you have 30 modules, but you've only overridden 25 of them, the remaining 5 will still work fine. They will just use the default TPL files.

If you want to override modules/bankwire/payment.tpl, modules/bankwire/payment_execution.tpl and modules/bankwire/payment_return.tpl, then you must copy these files to themes//modules/bankwire/payment.tpl, themes//modules/bankwire/payment_execution.tpl and themes//modules/bankwire/payment_return.tpl.

Link to comment
Share on other sites

And if I want only hide them (the 5 !), how must I proceed ? With CSS display none ? If yes, how ?
For example I have the theme2 and mydefaulttheme, I would like activate all module (30) in my default theme but only 25 in my theme2 (hide the last 5), how to do that ?

Link to comment
Share on other sites

I don't understand why you'd want to hide all of a module's TPL files. Why not just disable the module on the "Modules" tab? Yes, you could override all the TPL files with blank files to remove all the HTML, or if you want the HTML code there, but just have it hidden, then you will have to edit the CSS code and use display: none to hide it.

Link to comment
Share on other sites

I don't understand why you'd want to hide all of a module's TPL files. Why not just disable the module on the "Modules" tab? Yes, you could override all the TPL files with blank files to remove all the HTML, or if you want the HTML code there, but just have it hidden, then you will have to edit the CSS code and use display: none to hide it.


Because I want 30 modules on my first theme and 25 only on my second theme, if I disable, i will have 25 too on my first theme...you understand ?
For the css solution, where to insert the display none ? Witch file must I edit ? Where ?
Link to comment
Share on other sites

It's not possible to disable modules on a per-theme basis, at least not in PrestaShop v1.3.1. If you were using PrestaShop v1.4, you could use the "Import/export a theme" module, which lets you export your current theme and modules to a ZIP file that includes a config.xml file that specifies which modules should be enabled and disabled. Once you'd exported your two themes with the modules in the right state, you could simply import one of the themes and your modules will automatically be enabled or disabled as necessary. Unfortunately, PrestaShop v1.3.1 doesn't have this feature, so you are stuck manually enabling and disabling modules. I suppose overriding with blank TPL files is your only option.

Link to comment
Share on other sites

I don't understand. It's not possible to disable the "override" in PrestaShop. It's hardcoded in the core files on lines 616-625 of classes/Module.php (in PrestaShop v1.3.2):

if (Tools::file_exists_cache(_PS_THEME_DIR_.'modules/'.basename($file, '.php').'/'.$template))
{
   $smarty->assign('module_template_dir', _THEME_DIR_.'modules/'.basename($file, '.php').'/');
   $result = $smarty->fetch(_PS_THEME_DIR_.'modules/'.basename($file, '.php').'/'.$template);
}
elseif (Tools::file_exists_cache(dirname($file).'/'.$template))
{
   $smarty->assign('module_template_dir', __PS_BASE_URI__.'modules/'.basename($file, '.php').'/');
   $result = $smarty->fetch(dirname($file).'/'.$template);
}



The first if statement checks whether the module's TPL file has been overridden in the theme. If it has, then it reads it. If it hasn't, then it goes to the elseif statement and reads the original TPL file.

Link to comment
Share on other sites

rocky, you are a great man… this is wonderful guide

I want to ask a question;
is this development guide valid for ps 1.3.x? I mean, what is the differences between ver1.3 and ver1.4 for development? if you write shortly, I will be wery happy…

thank you

Link to comment
Share on other sites

This guide is for PrestaShop v1.4.2, not for PrestaShop v1.3.7 and earlier. PrestaShop v1.3.7 doesn't use controllers like PrestaShop v1.4 does. It has all the controller code in the files in the root directory, and it doesn't use config.xml files in its modules, or let you specify an author name. Otherwise, the guide should still apply.

Link to comment
Share on other sites

I am trying to figure out the correct way to override a class file. When i look into your guide you seem to use this code for overriding the controllers:

class CategoryController extends CategoryControllerCore
{
   public function process()
   {
       parent::process();

       $this->smarty->assign('HOOK_CATEGORY', Module::hookExec('category'));
   }
}



I have created a class override file for the frontcontroller core file :

class FrontController extends FrontControllerCore
{
   public function displayHeader()
   {
       self::$smarty->assign(array(
           'HOOK_HOMEBLOCKLEFT' => Module::hookExec('homeblockleft'),
           'HOOK_HOMEBLOCKMID' => Module::hookExec('homeblockmid'),
           'HOOK_HOMEBLOCKRIGHT' => Module::hookExec('homeblockright'),
           'HOOK_VANISEARCHBAR' => Module::hookExec('searchbar'),
           'HOOK_FOOTER1' => Module::hookExec('footer1'),
           'HOOK_FOOTER2' => Module::hookExec('footer2'),
           'HOOK_FOOTER3' => Module::hookExec('footer3'),
           'HOOK_FOOTER4' => Module::hookExec('footer4')
           ));

       parent::displayHeader();

   }



The difference is the order of the parent call. But when i put the parent call above the smarty assign then it does not work. So whats happening here? With the override code above the correct hooks are created and can be used. But when i select a payment method in the cart if gives a nasty error :
Fatal error: Call to a member function assign() on a non-object in D:\websites\142fin-afterupgrade\override\classes\FrontController.php on line 13

So obviously something is wrong, but what?

Link to comment
Share on other sites

If you see the original code, you can see theses lines at the beginning.

the method FrontController->init() will set some properties and var (like FrontControoler::smarty), checking if the ip is allowed, the cookie, if user is a customer, and that kind of thing.

As the displayHeader() method uses self::$smarty, we need to initialize it.

Link to comment
Share on other sites

Error in function displayErrors()

Hi i got this error caused by the foreach-loop and can´t fix it.

Warning: Invalid argument supplied for foreach() in J:\Web ... my.php on line 68

So what´s wrong there?

   public function displayErrors(){
       $errors =''
       .sizeof($this->_errors).' '.(sizeof($this->_errors) > 1 ? $this->l('errors') : $this->l('error'))
       .'';

       foreach ($this->_errors AS $error){
           $errors .= ''.$error.'';
       }

       $errors .= '';

       return $errors;
   }

Link to comment
Share on other sites

I've now finished the offline version of the PrestaShop Development Guide. It also includes a ZIP file of the "Hello World" module. I decided to charge a small amount, since I've put a lot of effort into the guide. You can purchase it here. As a bonus, I've included an offline version of the PrestaShop Performance Guide too. Of course, you can still read the guides online version for free.

Link to comment
Share on other sites

  • 2 weeks later...
  • 2 weeks later...

I don't understand. Do you mean you want to hide a module from all CMS pages or a specific one? You can add cms.php to hide the module from all CMS pages. If you want to hide it from a specific CMS page, you'll need to override the module's TPL file and add code like {if $page_name != 'cms' OR ($page_name == 'cms' AND $smarty.get.id_cms != 1)} at the top and {/if} at the bottom so hide the module from CMS page 1 only.

Link to comment
Share on other sites

  • 4 weeks later...
  • 5 weeks later...
  • 4 weeks later...
  • 2 weeks later...

Hi all,

I am a web developer and now work on prestashop but not have enough knowledge of prestashop.If any body know that there are feature for changing expiration date in admin area because i did not found. Else please help to how create new function to changing expiration date for product in prestashop.

Link to comment
Share on other sites

  • 2 weeks later...

Thanks Rocky, this is very helpful. One thing I couldn't find anywhere (tried google as well): how to include a conditional style sheet in header.tpl using a variable such as $tpl_dir?

I have tried

<!--[if lt IE 9]>

<link href="{$tpl_dir}css/ie.css" rel="stylesheet" type="text/css" media="{$media}" />

<![endif]-->

 

but it doesn't work. Any tip how to achieve this?

 

And I have another question. In the guide there is an explanation

"In PrestaShop, it is convention to put CSS blocks containing only one line on a single line, and the last line of a CSS block doesn't have a semicolon."

 

Why is this so? I find the missing semicolon at the end a bit irritating. It looks wrong somehow ...

 

Anyway thanks for this great guide. It helps a lot.

Link to comment
Share on other sites

  • 2 weeks later...

Very good rocky. ( too good for me ).

 

I just wanted to replace

 

header("Cache-Control: no-store, no-cache, must-revalidate");

 

with

 

header("Cache-Control: private, must-revalidate");

 

and do not understand where.

 

I'm @ bottom of the learning curve.

 

thanks for Guide and guidance

Link to comment
Share on other sites

  • 4 weeks later...

Thank you. Very nice help.

 

I have a question:

Administration, module page, if a module is installed, activate or deactivate appears.

Do you know which function is called with the link activate and deactivate ? I would like to add code when these links are called.

 

Thank you very much

Link to comment
Share on other sites

Rocky thanks a lot for your great guide to prestashop developers. I' ve been searchingr4 something like that for a year, since version 1.3.x... You have done a great job there. Thanks again

In future version you will be allowed to have several themes at the same times, with appropriates modules activated or not, but it's not available for now.
Link to comment
Share on other sites

  • 1 month later...
  • 2 weeks later...

Thanks Rocky. I am trying to locate each file in my default theme 1.4.7.0. But many of the files can not be found. I know you write about 1.4.2 version. So many files changed with the version, is it right? who can tell me a way to search each file in my computer which is installed version 1.4.7.0 default theme? I am a newbie, for me the first thing to do is to locate the files in my prestashop application. Thanks,

Link to comment
Share on other sites

I ask a basic question. Can I think each of Prestashop module is a class which is extended from a parent class named ObjectModel? Maybe it is a silly question. I am too new to ask a proper question. I just want to clarify some questions in my mind after I read some of this post. Actually I don't know what is ObjectModel and how to get the definition of it in PHP manual. Thanks for help.

Link to comment
Share on other sites

I am quite confused with the concepts of HOOK. There are php scripts, hook names with hook id, modules and smarty tpl files. I noticed one hook name can hook several modules and one module can be hooked with several hook names. And hook name can be inserted in any place of a tpl file. Who can tell the relations of above 4 concepts in simple words? Thank you for guide.

Link to comment
Share on other sites

  • 1 month later...
  • 4 weeks later...
  • 4 weeks later...

Hello.

I have an issue on my guest check out form.

My product is downloadable it means that many fields on the check out form are useless to me.

 

My question is. How can I switch off the validation for the required fields?

 

For example, the Guest Check out form is:

 

Instant check out

Email:

1º name:

Last name:

(I could manage to remove successfully the non-required fields)

Delivery address:

Again 1º name

Again last name

 

The issue is if I remove the 1º and last name from the instant check out form, I m able to make the purchased but when I go to the back office I got 1º/last name undefined. When I receive the confirmation email was “Hi, undefined ….” This is not professional.

 

And if I remove the name fields from the delivery address I can’t proceed, once the validation error comes in “First name and last name are required fields”.

 

I did the changes on themes/prestashop/order-opc-new-account.tpl

My prestashop is 1.4.8.2

 

Thanks

Link to comment
Share on other sites

I don't understand. Do you mean you want to hide a module from all CMS pages or a specific one? You can add cms.php to hide the module from all CMS pages. If you want to hide it from a specific CMS page, you'll need to override the module's TPL file and add code like {if $page_name != 'cms' OR ($page_name == 'cms' AND $smarty.get.id_cms != 1)} at the top and {/if} at the bottom so hide the module from CMS page 1 only.

Hi, I want to hide the right lateral hook from all the CMS pages, I did not quite understand what do you mean by "add cms.php to hide the module from all CMS page", could you be a little more specific please.

Thank you.

Link to comment
Share on other sites

This is much more useful than the official documentation. The official tutorial is great up to the part where you need to configure the module and save data to the database. It starts off explaining each line creating the module and then towards the end looks like they got tired and threw up some code for an admin tab and called it a day.

 

In any case, thanks for the info, it is very appreciated.

Link to comment
Share on other sites

hy, i'm new in prestashop

 

i try to adding module to link such as information like how to, guide for my shop

 

i've already successfull create some basic module, and hook it in the left coloumn,

 

my question is, how to make controller, for add link in my module to show my file php?

 

example my file php is "abc.php" in left colomn, already 3 li for each information

 

sorry for my bad english :(

Link to comment
Share on other sites

  • 1 month later...

Here's some constructive feedback of your development documentation...

 

It's grossly incomplete. There is zero documentation in any of the files that are part of the core as to exactly what they do, and there is NO documentation dedicated to the data flows, logic, or structure of the product as a whole.

 

While you believe it's useful to document how you *want* people to extend the product, sometimes that can not be done correctly (or at all) without understand what the pieces "around it" do and how they do it.

 

I need to make a change to the product that I do not know if I can do with a module because I have absolutely no idea what pieces get called at what point to do what at any point in the rendering of a page or movement through the product. So, if what I need can be done with a module, I have no idea. And, if it can't, I also have no idea where in the original code to go to possibly code a change.

 

I am not a "developer" by trade, but I am capable of writing code and understanding code written by others. With PrestaShop, however, I don't even know where to start in order to trace a process through the product.

 

PS appeared to be the perfect replacement to the much more complex (and slower) Magento. But, with the complete lack of documented knowledge around what was coded by the development team, it is no more useful to me.

  • Like 3
Link to comment
Share on other sites

  • 2 months later...

Hi guys

I use 2 things to help me out discover the prestashop structure when i need it : the file dbmodel, locacte in /docs/dev, wich you can open with mysql workbench

Also, most usefull, a doxygen generated doc, that i hosted at http://www.templeofdeejays.com/docs/doxygen/

 

But this, along as a lot of other stuff around documenting prestashop, should either be done by the team - or by the community on an official community developping site (drupal manages it very well, take some experience from it !)

  • Like 1
Link to comment
Share on other sites

  • 3 weeks later...
  • 3 weeks later...
  • 2 weeks later...
  • 2 weeks later...

I am trying to edit the index.php. It goes to config/config.php. How can i edit it and the css file. I want to add a different background.

 

 

I think that is not to discuss this topic, you should open another.

 

On the other hand, what questions you answer, and it must modify all you should modify the template instead of directly editing the index.php to change the background color,

Link to comment
Share on other sites

  • 2 weeks later...
  • 3 weeks later...
  • 4 weeks later...
  • 3 weeks later...
  • 1 month later...