Cart  

product (empty)

Main >> PrestaShop Tips and Modules

PrestaShop Module Development - 1

Who is this article for?

This article is meant for experienced PHP developers and people who familiar with web technologies in general. It is not an intro to programming or module development for PrestaShop. It is instead meant to explore the PrestaShop architecture and introduce how to access various elements within the API.

This article will contain basic module structure and hooks. Future articles will explore other elements in the PrestaShop API and how to accomplish certain tasks.

Basic Module Structure

A PrestaShop module is a PHP class that extends the PrestaShop core class Module. There are other core classes that extends the Module class, which can be used as a base as well. For instance the PaymentModule class is sued for payment modules.

The module is stored in a directory under modules directory. The name of the directory, the class name and the php file name must all be the same.

A basic module has the following structure:

 

<?
class NewModule extends Module{
function __construct(){
$this->name = 'newmodule';
$this->tab = 'Tools';
$this->version = '0.1';
parent::__construct();
$this->page = basename(__FILE__, '.php');
$this->displayName = $this->l('New Module');
$this->description = $this->l('A module that does some stuff.');
....
}

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

?>

The construct function on the parent is called to do some common initializations. The variables set contain the following information:

Name Name of the module's class.
Tab The tab in which the module should be put in the back office modules list.
Version Version of the module.
Page The path to the directory which the module is in.
DisplayName The name displayed in the module list in back office.
Description The description displayed in the back office.

 

 

The install function is called when the module is installed from the back office. The parent install function needs to be called to perform common installation tasks, such as adding the module to the database. other tasks such as creating required tables, registering a hook etc. should also be done here. These will be explained in more detail later.

The uninstall function is called on uninstall. Here you should remove the tables you created and the configuration variables you added. Calling the parent uninstall function will remove common module elements and remove the module from the database.

Templates and Languages

Prestashop uses smarty template engine. If you do not know about smarty, you should first read up on it before developing modules in Prestashop.

Prestashop uses template folders located under themes directory. Each theme folder has several smarty template files, for different pages, such as product page, category page etc. The same folder contains css files and theme specific javascript files. Modules can have different templates for each theme. If a theme does not contain the template your module uses, Prestashop defaults to the module's directory. This way you can distribute your templates with your module, and users can override it with their own versions in their theme directory.

There is a global $smarty variable in Prestashop, which you can use in any module as long as you reference to it. All files and modules use the same smarty object, so variables set before your module is called will be accessible within the template file.

Once you assign variables to the smarty object, you can call the "display" method of the Module class to handle the display of the template. The following is a short example of the usage:

 

global $smarty;
$smarty->assign('variable', $variable);
return $this->display(__FILE__, 'template.tpl');

 

__FILE__ constant points to the module file, which is used by Prestashop to locate the directory of the module, to look for the "template.tpl" file.

Prestashop provides support for multilingual content. From the start you should start developing your module to use multilanguage functions. The strings you use within the module are saved in language files in your module directory. There is a separate file for each language, named with iso code of the language and php extension. en.php, fr.php etc.

There are two places you will usually need to access multilanguage strings. The module php file, and the smarty templates you use with the module. Within module php file, you can call the Module class function "l" (L). This function will get the translated string from the current language file.

 

$this->L("string");

Within the smarty template, there is a simalar function registered. The function is again called "l" (L), with two parameters, "s" the string to be translated and "mod" the name of the module.

 

{l s="string" mod="module_name"}

 

The strings passed to these functions can be translated in the BO, translations->module translations.

Hooks

In various core classes and core files in PrestaShop, there are hooks where you can execute a function within your module. Your module will have access to global variables, and hook specific variables that are passed to it in an array.

You would want to use hooks for the following reasons:

    • Display information on the front end to the customers, in different locations, such as the homepage, left or right column, certain pages like payment, order confirmation etc. You can also use hooks to run Javascript on these places as well. It basically let's you display any HTML or text you want.
    • Display text or HTML to the employees in some places in the back end. Like Customers tab, or Orders tab. There is also stats tab where a list of stats modules is displayed. Stats module also execute certain Javascript code in the front end.
    • Run background operations on certain tasks. Like performing certain actions on order completion or when a payment is made etc. This might just be recording some data in the database or performing other background tasks. You can also execute these task for the backend.

 

Typically you would want to add your module to a hook by calling a registerHook function during the install. This function is declared in the Module class.The function is defined as:

 

public function registerHook($hook_name)

 

The following will register the module to the home page:

 

$this->registerHook('home');

 

Whenever the homepage is displayed the following function will be called in your module:

 

public function hookHome($params)

 

The function name is always hook. The $params variable is an array containing some global variables, like cart ($params['cart']) and some hook specific variables. Like order id (id_order) on order confirmation.

"home" is the hook that is called in the homepage. You can use this to print some content on the center column on the home page, or execute any other code. The function will be called everytime the homepage is displayed.

The hooks are stored in a table called ps_hooks. Here is a list of hooks available in the default installation of PrestaShop v 1.3:

Name of the hook Title Description A positionable hook?
payment Payment

This hook is a special hook. It is called on a payment module, the returned string is printed on the list of available payment methods on the checkout.

Yes
newOrder New orders
No
paymentConfirm Payment confirmation
No
paymentReturn Payment return
No
updateQuantity Quantity update Quantity is updated only when the customer effectively place his order. No
rightColumn Right column blocks
Yes
leftColumn Left column blocks
Yes
home Homepage content
Yes
header Header of pages A hook which allow you to do things in the header of each pages. This is the first hook executed on a front page. You can use this to insert elements between head tags.
Yes
cart Cart creation and update Whenever a product is added or removed, this hook is executed.
No
authentication Successful customer authentication
No
addproduct Product creation
No
updateproduct Product Update
No
top Top of pages

A hook which allow you to do things a the top of each pages. This is the hook that displays page elements on top, near the shop logo. Quick search, currencies, languages, site links etc. are all in this section.

Yes
extraRight Extra actions on the product page (right column). This will let you put content under the section on the right of the product image on the product page. This won't be enclosed in the same section as the "add to cart" button.
No
deleteproduct Product deletion This hook is called when a product is deleted No
productfooter Product footer Add new blocks under the product description Yes
invoice Invoice Add blocks to invoice (order) Yes
updateOrderStatus Order's status update event Launch modules when the order's status of an order change. No
adminOrder Display in Back-Office, tab AdminOrder Launch modules when the tab AdminOrder is displayed on back-office. No
footer Footer Add block in footer of the page.
Yes
PDFInvoice PDF Invoice Allow the display of extra informations into the PDF invoice No
adminCustomers Display in Back-Office, tab AdminCustomers Launch modules when the tab AdminCustomers is displayed on back-office. No
orderConfirmation Order confirmation page Called on order confirmation page No
createAccount Successful customer create account Called when new customer create account successfuled No
customerAccount Customer account page display in front office Display on page account of the customer Yes
orderSlip Called when a order slip is created Called when a quantity of one product change in an order. No
productTab Tabs on product page

Called on order product page tabs. You can use this to display the tab title. The returned HTML shuold be in this format:

<a href="#<id of the content div>">Tab Title</a>

No
productTabContent Content of tabs on product page

 

Called on order product page tabs. This will display the content of the tab. The content needs to be enclosed in a div with the id specified in the productTab hook.

 

 

<div id="<id of the content div>">Tab Content</div>

 

No
shoppingCart Shopping cart footer Display some specific informations on the shopping cart page No
createAccountForm Customer account creation form Display some information on the form to create a customer account Yes
AdminStatsModules Stats - Modules
Yes
GraphEngine Graph Engines
No
orderReturn Product returned
No
productActions Product actions Put new action buttons on product page. This hook will let you put content in the "add to cart" button area on each product. You can use this to add additional buttons that can perform other actions. Yes
backOfficeHome Administration panel homepage
Yes
GridEngine Grid Engines
No
watermark Watermark
No
cancelProduct Product cancelled This hook is called when you cancel a product in an order No
extraLeft Extra actions on the product page (left column).

This hook let's you put extra action links on the product page, right under the product images, like the "Print" link.

No
productOutOfStock Product out of stock Make action while product is out of stock Yes
updateProductAttribute Product attribute update
No
extraCarrier Extra carrier (module mode)
No
shoppingCartExtra Shopping cart extra button Display some specific informations Yes
search Search
No
backBeforePayment Redirect in order process Redirect user to the module instead of displaying payment modules No
updateCarrier Carrier Update This hook is called when a carrier is updated No
postUpdateOrderStatus Post update of order status
No
createAccountTop Block above the form for create an account
Yes
backOfficeHeader Administration panel header
No
backOfficeTop Administration panel hover the tabs
Yes
backOfficeFooter Administration panel footer
Yes
myAccountBlock My account block Display extra informations inside the "my account" block Yes
<li><a class="selected" id="more_info_tab_more_info" href="#idTab1">More infoa>li>	
Author: Burhan BAVKIR


Share |

HomeHome