Overriding Files

PrestaShop lets you override controllers and classes in the /override directory and override module templates in the theme. This lets you modify code without actually modifying the original files, which makes it easier to upgrade to newer versions of PrestaShop in the future, since you don’t have to worry about overwriting code changes.

Overriding Classes

To override any of PrestaShop’s classes, create a file in the /override/classes directory with the same name as the class. See Archiecture > Models for a list of classes you can override and a description of what each class does.

As an example, you can add a date_add field to the ps_cms table, then override the CMS class to add a publish date to CMS pages by creating the file /override/classes/CMS.php and inserting the following into the file:

<?php
class CMS extends CMSCore
{
    public date_add;
    public function getFields()
    {
        $fields = parent::getFields();
        $fields['date_add'] = pSQL($this->date_add);
        return $fields;
    }
}
?>

The extends part of the class line defines which class is being overridden. It is important to use the class name, not the filename of the class. The override’s class name can be anything, as long as it is a valid class name. If you plan to share this class with others, you should use a unique name to make it easier to put multiple classes in the file.

The public date_add line adds a new date_add member that can be accessed using $cms_page->date_add. The class overrides the getFields() function to get the existing non-translatable fields, add the date_add member to the field list, then return them. By doing this, the date_add variable will be read whenever an CMS object is initialised and written whenever it is saved.

Overriding Controllers

To override any of PrestaShop’s controllers, create a file in the override/controllers directory with the same name as the controller. See Architecture > Controllers for a list of controllers you can override and what each controller does.

As an example, you can override the category listings to add a new hook for modules to use by creating the file override/controllers/CategoryController.php and inserting the following code into the file:

<?php
class CategoryController extends CategoryControllerCore
{
    public function process()
    {
        parent::process();
        self::$smarty->assign('HOOK_CATEGORY', Module::hookExec('category'));
    }
}
?>

The extends part of the class line defines which controller is being overridden. It is important to use the class name of the controller, not the filename of the controller. The override's class name should be the controller's class name without Core.

The function line defines which function is being overridden. See Architecture > Controllers for a list of controller functions that can be overridden and descriptions of each function. In this example, it is the process() function being overridden, which is called before displaying the content. The parent line is used to call the core controller’s process() function before running the override code. This is important if you want to simply add code to PrestaShop’s existing code. If you leave out this line, you will be completely overriding PrestaShop’s code, so only your own code will run.

The smarty line creates a new Smarty variable called HOOK_CATEGORY, then calls all modules that are installed in the category hook. The variable {$HOOK_CATEGORY} can then be used in product-list.tpl to add module code.

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/<yourtheme>/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.

Overriding Back Office Tabs

It is possible to override Back Office tabs in PrestaShop or create new ones using modules. To do this, copy the existing tab from the /admin/tabs directory or create your own tab inside a module’s directory. It doesn’t have to be a working module’s directory. You could, in fact, just create an /override directory inside the /modules directory and copy all your overridden tabs there.

Once you’ve done that, go to the Employees > Tabs tab and edit the tab you want to override, or create a new tab. Enter the module’s directory name in the “Module” field, then click “Save”. The original tab should then be replaced with the one in the module’s directory, or the new tab should be displayed, but it won’t have an icon. To add an icon, you must copy the tab’s icon from the /img/t directory to the module’s directory or create your own icon. The icon must have the same name as the tab and must be a GIF image.

There are some tabs like the product editor AdminProducts.php that are not listed on the Employees > Tabs tab, so you cannot override them directly. To override them, you must override the tab that calls them. For example, to override AdminProducts.php, you must override AdminCatalog.php and change the includes to link to your module’s directory:

include_once(PS_ADMIN_DIR.'/../classes/AdminTab.php');
/*** Original Code
include(PS_ADMIN_DIR.'/tabs/AdminCategories.php');
include(PS_ADMIN_DIR.'/tabs/AdminProducts.php');
***/
/*** Start of AJAX Sliding Categories Changes ***/
include(PS_ADMIN_DIR.'/../modules/blockcategoriesnc/AdminCategories.php');
include(PS_ADMIN_DIR.'/../modules/blockcategoriesnc/AdminProducts.php');
/*** End of AJAX Sliding Categories Changes ***/

Last updated