Introduction
Very often users find default Prestashop functionality quite insufficient. At the same time, although you can expand the functionality by making changes in core files, later you may have some difficulties since you will not be able to update your Prestashop version and buying new modules and addons can become a nightmare for you, because the modified part of the system may not run after you install a new module. There is a special method called override which helps to solve this type of problem. So, what is override? This is a mechanism which allows you to change behavior of different functions without changing Prestashop core files themselves. Let’s see how it works: as we have previously described, the collector Autoloader gathers classes’ files. It is searching for files as follows:
- classes/
- override/classes/
- controllers/
- override/controllers/
In other words, if your class is located in the override folder, instead of the standard file your customized file will be used, which as a rule has been inherited from the default core class.
Overriding classes
The file Classes/Autoload.php is responsible for the order of loading files and the function generateIndex() contains the logic for loading classes.
1 2 3 4 5 6 7 |
$classes = array_merge( $this->getClassesFromDir('classes/'), $this->getClassesFromDir('override/classes/'), $this->getClassesFromDir('controllers/'), $this->getClassesFromDir('override/controllers/') ); ksort($classes); |
Overriding modules’ tpl files
To make changes into a module’s tpl file there is no need to change the file itself. All you need is to use the advantages of the override method and place the modified file into the folder with your frontend theme. For example, to override the file blockwishlist/blockwishlist.tpl you need to create this file: themes/your_theme/modules/blockwishlist/blockwishlist.tpl
Overriding admin panel themes
override/controllers/admin/templates – this is the folder where you can place modified admin theme files. If you want to replace an admin theme file in your own module – there is no such automatic collector as used for classes (see the section “ How to make override in your own module” the function instаllOverrides()). There is no such possibility for a frontend theme as to overload a theme.
If you generate forms using the Helper class, please, note the function Helper::createTemplate()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
public function createTemplate($tpl_name) { // a property has been set override_folder if ($this->override_folder) { //if the module expands not AdminController but ModuleAdminController if ($this->context->controller instanceof ModuleAdminController) $override_tpl_path = $this->context->controller->getTemplatePath().$this->override_folder.$this->base_folder.$tpl_name; else if ($this->module) { //if the object has the Module property and contains in it the object of the module $override_tpl_path = _PS_MODULE_DIR_.$this->module->name.'/views/templates/admin/_configure/'.$this->override_folder.$this->base_folder.$tpl_name; } else { if (file_exists($this->context->smarty->getTemplateDir(1).DIRECTORY_SEPARATOR.$this->override_folder.$this->base_folder.$tpl_name)) $override_tpl_path = $this->context->smarty->getTemplateDir(1).DIRECTORY_SEPARATOR.$this->override_folder.$this->base_folder.$tpl_name; else if (file_exists($this->context->smarty->getTemplateDir(0).DIRECTORY_SEPARATOR.'controllers'.DIRECTORY_SEPARATOR.$this->override_folder.$this->base_folder.$tpl_name)) $override_tpl_path = $this->context->smarty->getTemplateDir(0).'controllers'.DIRECTORY_SEPARATOR.$this->override_folder.$this->base_folder.$tpl_name; } } else if ($this->module) { $override_tpl_path = _PS_MODULE_DIR_.$this->module->name.'/views/templates/admin/_configure/'.$this->base_folder.$tpl_name; } //verifies the file existence. If the file does not exist under the new path, it uses the standard one instead if (isset($override_tpl_path) && file_exists($override_tpl_path)) return $this->context->smarty->createTemplate($override_tpl_path, $this->context->smarty); else return $this->context->smarty->createTemplate($this->base_folder.$tpl_name, $this->context->smarty); } |
How to do override in admin panel – http://belvg.com/blog/overloading-a-template-in-your-admin-panel-controller.html
How to do override in your module – http://belvg.com/blog/how-to-make-an-override-in-your-module.html