Prestashop - override function in existing prestashop module
Asked Answered
F

5

7

I would like to change existing prestashop module without copying it and creating new one. I know that it is possible to override .tpl files in prestashop, but is it possible to do same thing with php classes? For instance I would like to change blockcart so that it can be hooked on top. Since original version doesnt have that hook I need to change install() function! I can`t change original source (it would be bad idea isn't it...) file I need to override install() function by inheriting blockcart module. Is it possible to do so and where I can find example?

Fonteyn answered 26/5, 2011 at 15:51 Comment(0)
M
11

I use my own override to the FrontController class to allow the display of module output at arbitrary points in tpl files - this means that the module doesn't need to support a particular hook. It is implemented via a smarty plugin, so you can for example use:

{plugin module='blockcart' hook='rightColumn'}

The above will force the module to output what it would display if hooked to the right column where the above is tag inserted (which can be anywhere in any tpl file). You can "unhook" the module from the right column so that it only displays where you want it to using this technique. I have used it on a production site with great success.

There's a series of articles describing how it works (with the required code) available at:

Prestashop 1.4 Plugins

Munch answered 2/9, 2011 at 22:54 Comment(3)
I've always felt that while the Modules and hooks system works in many cases, there are some times where it's just too inflexible. Glad you found it useful.Munch
Excellent Paul! By the way can I pass some parameters to the module's hook? Thanks!Mandler
Absolutely you can using args= it does have to be an array though.Munch
P
2

In Prestashop 1.4 you can override core classes and module templates Today this is not possible to override a module php file but we are working on it.

Potvaliant answered 31/8, 2011 at 9:20 Comment(0)
E
2

in override\modules\blockcart\blockcart.php (create it if it does not exist yet)

<?php 
    class BlockCartOverride extends BlockCart
    {
        public function hookDisplayTop($params)
        {
            return parent::hookTop($params);
        }
    }
?>

like this you can override any module to be hookable on any default or custom hook. don't forget to delete cache/class_index.php for the override to work :)

Esthonia answered 15/5, 2018 at 13:44 Comment(0)
I
0

Since version 1.6.0.11 of PrestaShop, there is a new feature that allows developers to override a module’s instance classes.

Override a module’s instance class by extending it To override a module’s instance class, you have to extend it, giving the extended class the same name and adding Override suffix:

<?php
if (!defined('_PS_VERSION_'))
    exit;
class BlockUserInfoOverride extends BlockUserInfo
{
    public function hookDisplayNav($params)
    {
        return '<div class="header_user_info"><a>Test</a></div>';
        // return $this->display(__FILE__, 'nav.tpl');
    }
}

Source: http://build.prestashop.com/howtos/module/how-to-override-modules/

Irena answered 30/5, 2016 at 22:10 Comment(0)
V
0

Keep in mind that in 1.7.x era - nowadays - you can override module main classes but not controllers. To be able to override controllers you have to override the core classes (to detect any possible overrides) and then do whatever you like. Alternatively, you have to get the original files as backup and put the modified in the same place on install and the reverse procedure on uninstall.

Visual answered 8/9, 2020 at 22:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.