Diving in PrestaShop Core

Fundamentals

Concepts

 

You should be familiar with PHP and Object-Oriented Programming before attempting to write your own module.

PrestaShop was conceived so that third-party modules could easily build upon its foundations, making it an extremely customizable e-commerce software.

A module is an extension to PrestaShop that enables any developer to add the following:

The company behind PrestaShop provides more than 100 modules for free with the tool itself, enabling you to launch your business quickly and for free.

More than 1600 add-ons are also available at the official add-ons site.
These additional modules were built by the PrestaShop company or members of the PrestaShop community, and are sold at affordable prices.
As a developer, you can also share your modules on this site, and receive 70% of the amounts associated with the sale of your creations. Sign up now!

PrestaShop's technical architecture

PrestaShop is based on a 3-tier architecture:

This is the same principle as the Model–View–Controller (MVC) architecture, only in a simpler and more accessible way.

Our developer team chose not to use a PHP framework, such as Zend Framework, Symfony or CakePHP, so as to allow for better readability, and thus faster editing.

This also makes for better performances, since the software is only made of the lines of code it requires, and does not contain a bunch of supplemental generic libraries.

A 3-tier architecture has many advantages:

Model

A model represents the application's behavior: data processing, database interaction, etc.

It describes or contains the data that have been processed by the application. It manages this data and guarantees its integrity.

View

A view is the interface with which the user interacts.

Its first role is to display the data that is been provided by the model. Its second role is to handle all the actions from the user (mouse click, element selection, buttons, etc.), and send these events to the controller.

The view does not do any processing; it only displays the result of the processing performed by the model, and interacts with the user.

Controller

The controller manages synchronization events between the Model and the View, and updates both as needed. It receives all the user events and triggers the actions to perform.

If an action needs data to be changed, the Controller will "ask" the Model to change the data, and in turn the Model will notify the View that the data has been changed, so that the View can update itself.

The PrestaShop file structure

The PrestaShop developers have done their best to clearly and intuitively separate the various parts of the software.

Here is how the files are organized:

About the cache

The /cache/Class_index.php file contains the link between the class and the declaration file. It can be safely deleted.

The /cache/xml folder contains the list of all the base modules.

When the store's front-end doesn't quite reflect your changes and emptying the browser's cache is not effective, you should try emptying the following folders:

PrestaShop's context

The Context is a technical feature introduced with version 1.5 of PrestaShop. Its two goals are:

The Context is a light implementation of the Registry design pattern: it is a class that stores the main PrestaShop information, such as the current cookie, the customer, the employee, the cart, Smarty, etc.

Before version 1.5, you had to rely on the cookie in order to access this data:

$cookie->id_lang;

Now that the Context is available, the same data can be accessed more cleanly:

$this->context->language->id;

You can read more about the Context in the "Using the Context Object" page of the Developer Guide.

Accessing the database

The database structure

PrestaShop's database tables start with the ps_ prefix. Note that this can be customized during installation

All table names are in lowercase, and words are separated with an underscore character ("_").

When an table established the links between two entities, the names of both entities are mentioned in the table's name. For instance, ps_category_product links products to their category.

A few details to note:

The ObjectModel class

This is the main object in PrestaShop object model. It can be overridden with precaution.

Defining the model

You must use the $definition static variable in order to define the model.

For instance:

/**
* Example from the CMS model (CMSCore) 
*/
public static $definition = array(
  'table' => 'cms',
  'primary' => 'id_cms',
  'multilang' => true,
  'fields' => array(
    'id_cms_category'  => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
    'position'         => array('type' => self::TYPE_INT),
    'active'           => array('type' => self::TYPE_BOOL),
    // Lang fields
    'meta_description' => 
        array('type'=>self::TYPE_STRING, 'lang'=>true, 'validate'=>'isGenericName', 'size'=>255),
    'meta_keywords'    => 
        array('type'=>self::TYPE_STRING, 'lang'=>true, 'validate'=>'isGenericName', 'size'=>255),
    'meta_title'       => 
        array('type' =>self::TYPE_STRING, 'lang'=>true, 'validate'=>'isGenericName', 'required'=>true, 'size'=>128),
    'link_rewrite'     =>
        array('type' => self::TYPE_STRING, 'lang'=>true, 'validate'=>'isLinkRewrite', 'required'=>true, 'size'=>128),
    'content'          => 
        array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isString', 'size' => 3999999999999),
  ),
);

A model for many stores and/or languages

In order to have an object in many languages:

'multilang' => true

In order to have an object depending on the current store

'multishop' => true

In order to have an object which depend on the current store, and in many languages:

'multilang_shop' => true

The main methods

Any overriding of its methods is bound to influence how all the other classes and methods act. Use with care.

Method name and parameters

Description

getValidationRules($className = _CLASS_)

Return object validation rules (fields validity).

getFields()

Prepare fields for ObjectModel class (add, update).

__construct($id = NULL, $id_lang = NULL)

Build object.

save($nullValues = false, $autodate = true)

Save current object to database (add or update).

add($autodate = true, $nullValues = false)

Save current object to database (add or update).

add($autodate = true, $nullValues = false)

Add current object to database.

update($nullValues = false)

Update current object to database.

delete()

Delete current object from database.

deleteSelection($selection)

Delete several objects from database.

toggleStatus()

Toggle object status in database.

validateFields($die = true, $errorReturn = false)

Check for fields validity before database interaction.

The DBQuery class

The DBQuery class is a query builder, which helps you creation SQL queries. For instance:

$sql = new DbQuery();
$sql->select('*');
$sql->from('cms', 'c');
$sql->innerJoin('cms_lang', 'l', 'c.id_cms = l.id_cms AND l.id_lang = '.(int)$id_lang);
$sql->where('c.active = 1');
$sql->orderBy('position');
return Db::getInstance()->executeS($sql

 

 

'multilang' => true