Child pages
  • Overriding default behaviors

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

Classes and controllers are usually built following a certain norm. Here is the the Product class and controller:

...

The file can be placed in either of these locations:

  • /override/classes/Product.php
  • /modules/my_module/override/classes/Product.php

Overriding a controller

In order to override the ProductController class, your file needs to be called ProductController.php and must feature a ProductController class that then extends ProductControllerCore class.

The file can be placed in either of these locations:

  • /override/controllers/front/ProductController.php
  • /modules/my_module/override/controllers/front/ProductController.php

Overriding other behaviors

...

  • /themes/my_theme/modules/my_module/my_module.tpl
  • /themes/my_theme/css/modules/my_modulesmodule/my_module.css
  • /themes/my_theme/js/modules/my_modulesmodule/my_module.js

Since PrestaShop 1.5, the path is slightly longer

  • /themes/my_theme/modules/my_module/views/templates/front/my_module.tpl
  • /themes/my_theme/css/modules/my_modulesmodule/views/templates/front/my_module.css
  • /themes/my_theme/js/modules/my_modulesmodule/views/templates/front/my_module.js

...

Note

Contrary to the override code that is to be placed manually in the /override folder, module overrides are enabled as soon as the module is installed. During installation, overriding code is merge with those already in place (if any), otherwise they are copied to the /override folder at the root of the PrestaShop folder.

Manipulating the override code manually

Modules and themes may add an override to a default behavior, and PrestaShop takes care of reseting the /cache/class_index.php file.

But sometimes you need to add that overriding code yourself, manually uploading the file to your server. In that case, you need to trigger the regeneration of the /cache/class_index.php file yourself. This is done simply by deleting the file: if PrestaShop cannot find the file, it will regenerate it, taking all the overrides into account.

It is the same when manually removing an override: in order to reinstate the default behavior, you must delete the /cache/class_index.php file.

Sample code

Example 1

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

...

Code Block
/*
 * With this override, you have a new Smarty variable called "currentController" available in header.tpl
 * This allows you to use a different header if you are on a product page, category page or home.
 */
class FrontController extends FrontControllerCore {
	public function displayHeaderinitHeader()
	{
		self::$smarty->assign('currentController', get_class($this));
		return parent::displayHeaderinitHeader();
	}
}