
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();