How to (finally) override Prestashop Modules’ core files

Without even letting us know, the Prestashop team finally made it possible to override Prestashop Modules’ core files in version 1.6.0.11. Let’s see how!

  • Prestashop version: 1.6.0.11 and above

How to (finally) override Prestashop Modules’ core files

Yes, we (at last!) can now override modules’ php files without messing with the core itself. It seems this mod has been around for a few days now, and the prestashop team, of course, didn’t let anyone now. I’d like to thank the Prestashop forum user Razaro who spotted this first.

Enough chitchatting. Let’s see how to override a Prestashop Module’s core. Reach the new override/modules/ folder. For this demonstration we will override the cms block, just to illustrate the purpose. Create a new folder called blockcms, and inside it a file named blockcms.php. Here is how modules overrides work:

<?php

if (!defined('_CAN_LOAD_FILES_'))
	exit;

class BlockCmsOverride extends BlockCms
{


}

The pattern is ModuleNameOverride. So in this case BlockCmsOverride. If we wanted to extend the top menu, it would have been BlockTopMenuOverride, and so on. Pretty easy to remember. Now let’s go ahead and simply add a var dump to make sure it works:


<?php


if (!defined('_CAN_LOAD_FILES_'))
	exit;

class BlockCmsOverride extends BlockCms
{
	public function displayBlockCMS($column)
	{
		var_dump('testing');
		if (!$this->isCached('blockcms.tpl', $this->getCacheId($column)))
		{
			$cms_titles = BlockCMSModel::getCMSTitles($column);

			$this->smarty->assign(array(
				'block' => 1,
				'cms_titles' => $cms_titles,
				'contact_url' => (_PS_VERSION_ >= 1.5) ? 'contact' : 'contact-form'
			));
		}
		return $this->display(__FILE__, 'blockcms.tpl', $this->getCacheId($column));
	}

}


Nothing fancy, it doesn’t do anything but it’s great to understand how the thing works: as with all overrides, you just have to replicate a method’s name to extend or replace it. We can also call the parent if we want, and make things more concise:


<?php


if (!defined('_CAN_LOAD_FILES_'))
	exit;

class BlockCmsOverride extends BlockCms
{
	public function displayBlockCMS($column)
	{
		var_dump('testing');
		parent::displayBlockCMS($column);
	}

}


As simple as that! Think about the possibilities, and the flexibility this gives to theme developers. Of course make sure you clear the class_index./php file located in the cache/ folder before testing it out!

And of course, like normal overrides, we can just create the very same “blockcms” folder in our third party module, so it will be automatically added when installed, again, like classes and controllers overrides.

Last mention

Since this silence towards the community is getting pretty annoying, I hope the Prestashop team will soon try to have more consideration for the user and developers base, informing us when such a new feature is released. Just a small code snippet for them:

if($prestashop - $community)
	die();

:)

You like the tuts and want to say "thank you"? Well, you can always feel free to donate:

  • jacqbus

    Perfect, but should i use that way to override .js and .css files? I mean i’ve tried override css and js in this way: http://doc.prestashop.com/display/PS16/Overriding+default+behaviors but it’s not working (with deleting class_index.php ofc.).
    My bypass is to override “favouriteproducts.php” like in this article, and change “$this->_path” in “hookDisplayHeader()” method to new path.

    And again – is this proper way, or maybe there’s any simpler way to override just few js/css files?

    • NemoPS

      THat doc is wrong :)
      Both css and js can be overridden in the main folder. Like

      theme/css/mobulename/cssfile.css

      And js in the same way, using the js folder.

      • jacqbus

        Hey NemoPS
        Thanks for reply – it helped me a lot :)

        I’m begginer at prestashop and i thought official documentation should be best place to begin learning. As we all can see in this example – is not :/

        So i wonder is there any better place (webpage, forum, wiki, something like that), where i can find more useful and primarily working tips and examples about prestashop?

        • NemoPS

          My Site! :D
          Jokes apart, the forum I guess.

        • jacqbus

          Ok Thanks for help :)

  • Vincent

    Great article! Thank you!

  • bart0356

    Is it possible to override a class of module, for example in module vatnumber i wish to override the VATNumberTaxManager class, possible ?

    • NemoPS

      I don’t think it is since they’re not being loaded using the same methods across the site, but you can give it a try with some

      • bart0356

        I have tried but no effect, more strange :
        If i override a module such as BlockLayered, all is ok.
        If i override the vatnumber module, it does not work, still load the old one.

        Any ideas ?

  • nicooprat

    Many thanks for sharing this with us. Still impossible to find that information in the official docs !

  • nicooprat

    This feature has been introduced in a more recent version : 1.6.11 (read the first line of the article).

  • skinnybloke

    Hi – excellent piece of work!

    One question – I want to use this to hook displayable content into my own hooks which is easy to do…

    …but can I use a module override to remove content being displayed in the hooks specified in the original module?

    If so, how? Or can it only be done via the the back office > modules > positions tab?

  • Rubens

    Hi,

    Thank you for this contribution. This feature is very useful and will surely bring thousands of new possibilities.

    I’ve tried to create a custom core override located in mymodulecontrollersadminAdminOrdersController.php

    It works pretty well when I install my module, and also – it has been removed – when I uninstall my module. The problem is that if do any change, it does not reflect to the override file in rootoverridecontrollersadminAdminOrdersController.php

    Should I really have to manually update this file whenever I want to change something?

    Just in case, yes, I already tried unsuccessful to delete the cacheclass_index.php

    Thank you,
    Rubens

    • NemoPS

      Just answered on Arvixe :) You basically have to reset the module every time so it picks up the override file again from the mod’s folder

  • http://ecigopaline.fr Zythom

    Hi,

    i tried to use your tuto to add a hook that will not be
    removed after an update in my blocknewsletter module but it doesn’t
    work. Could you tell me what i have done wrong please ?
    Here is the short code :

    hookDisplayLeftColumn($params);
    }
    }

  • WIC

    Thanks !

  • wayann

    Ciao Fabio, your prestashop tuts are well structured and easily graspable, if you want to understand quickly how prestashop works “inside” your tuts are an highway to productivity. Thank you for sharing with us.
    –Wayann

  • http://pelechano.es Enrique Gómez

    Great trick! Thanks for the info.
    But that’s the problem with Prestashop development, people has to find those improvements by itself. It seems that they are not interested in giving a good development documentation…

    “I hope the Prestashop team will soon try to have more consideration for
    the user and developers base, informing us when such a new feature is
    released” -> +1000000 :-)

Store Top Sales

You like the tuts and want to say "thank you"? Well, you can always feel free to donate: