Skip to end of metadata
Go to start of metadata

Modules, Classes and Controller Override

This article was written by Julien Breux, and was first published on August 10th, 2011, on the PrestaShop blog.

Introduction

PrestaShop allows you to override various components and behaviors. PrestaShop version 1.4 consists of two major points:

  • The first is overriding the visible parts of modules (Templates, JavaScript and style sheet language) so the themes can adapt better to them.
  • The second is overriding software behavior (class files and controller files) to target a specific section of the required components.

Module override

The modules are usually in the following format:

  • /modules/my_module/my_module.tpl
  • /modules/my_module/my_module.css
  • /modules/my_module/my_module.js

PrestaShop allows you to override or replace certain visible module files with new ones with the same theme. It couldn't be simpler, just do the following:

  • /themes/prestashop/modules/my_module/my_module.tpl
  • /themes/prestashop/css/modules/my_modules/my_module.css
  • /themes/prestashop/js/modules/my_modules/my_module.js

The new files will be used when you display your website.

Class override

Overriding is a way to "override" class files and controller files.
PrestaShop's ingenious class auto-loading function makes the "switch" to other files fairly simple.
Use inheritance to modify and add new behavior using the various classes' properties and methods.

They are usually constructed the following way (product example):

  • /classes/Product.php
    This class would be called "ProductCore"
  • /controllers/ProductController.php
    This controller would be called "ProductControllerCore"

You will need to create a file in the "override/classes/" folder to "override" the resulting class model such as:

  • /override/classes/Product.php

This class would be called "Product" and spans the "ProductCore" class. Just a flick of PrestaShop's magic wand and the spell is working!

You can also use this principle with the controllers and "override" in the following way:

  • /override/controllers/ProductController.php

This controller would be called "ProductController" and spans the "ProductControllerCore" class.

PrestaShop has certain folders you can use to override elements such as displaying redirections (_Tools.php) and measuring hook execution time (_Module.php) etc.

Example 1

Using data class MySQL.php is simply impossible while trying to type data into a database different from PrestaShop on the same MySQL Server. (Really!)

The solution is to use the following override of the MySQLCore class:

To use it you have to instantiate the class as follows:

  • For local connection : new MySQL(DB_SERVER, DB_USER, DB_PASSWD, 'DB_name', true);
  • For remote connection : new MySQL(DB_SERVER, DB_USER, DB_PASSWD, 'DB_name', true);

The last parameter forces the creation of a MySQL connection.

Example 2

Example 3

Example 4

Conclusions

Core files are not modified by overriding. This technique allows you to personalize your PrestaShop boutique and monitor how the software evolves. Updates are facilitated.

  • No labels