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 yo uthr 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]
<?php

if (!defined(‘_CAN_LOAD_FILES_’))
exit;

class BlockCmsOverride extends BlockCms
{

}

[/php]

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]

<?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));
}

}

[/php]

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]

<?php

if (!defined(‘_CAN_LOAD_FILES_’))
exit;

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

}

[/php]

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.

Need PrestaShop Modules? Have a look at my Prestashop Addons Store!

Looking for quality PrestaShop Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , | Posted under PrestaShop | RSS 2.0

Author Spotlight

Fabio Porta

Fabio has been involved in web development and design since 2005, when launched his first website at the age of 16. He’s now highly skilled in both client and server side development, along with design, and since August 2012 runs a successful website about PrestaShop tutorials and Prestashop Modules called Nemo’s Post Scriptum, at http://nemops.com

2 Comments on How to (finally) override Prestashop Modules’ core files

  1. Rubens says:

    Hi Fabio,

    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 mymodule\controllers\admin\AdminOrdersController.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 root\override\controllers\admin\AdminOrdersController.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 cache\class_index.php

    Thank you,
    Rubens

    • Fabio Porta says:

      Thanks for your comment!
      Actually, you need to reset the module every time you modify the override file in the module’s folder. What I usually do during the development stage is working directly in the main override folder, and move the necessary code once I’m done 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *