On 2/25/2015 at 2:02 PM, theillo said:BAM found a perfect solution!
I created a module that does that (so when I reinstall my store at a later point again, I can just reinstall the module). But you can also just hack your way through the process. I guess another advantage of making it a module is that you can require this module to be installed by all those modules that will now depend on this hook.
Here's what you need to do:
1.) Create a new Hook
When doing a module installation, you should be able to just call
$this->registerHook('nameOfNewHook');since the Hook class has a case for creating new, non existing hooks.
Otherwise, the code to create a new hook is this:
$hook = 'nameOfNewHook'; $new_hook = new Hook(); $new_hook->name = pSQL($hook); $new_hook->title = pSQL($hook); $new_hook->add();So just run that code during installation of your module, or just run it once and then you're set.
No I actually gave my new hook the name 'actionLoadProductTemplate'.
2.) Create a new Product Controller class
Create a file called ProductController.php with the following contents:
<?php class ProductController extends ProductControllerCore { public function initContent() { parent::initContent(); $template_file = Hook::exec('actionLoadProductTemplate', array('product' => $this->product, 'category'=> $this->category)); //you can pass along other properties of the product class by putting them in this array if($template_file && file_exists($template_file)) { $tpl_path = $template_file; } else { $tpl_path = _PS_THEME_DIR_.'product.tpl'; } $this->setTemplate($tpl_path); } }3.) Override the ProductConroller class
Once you created the class file, you'll need to make sure Prestashop uses it, by overriding it:
If you are installing a module, put it in the folder
'PS_ROOT/modules/NAMEOFYOURMODULE/override/controller/front/ProductController.php'
If you delete the file 'PS_ROOT/cache/class_index.php' JUST before the installation, it should even work properly from the start.
Of course alternatively you can just place your file in the override directory of the prestashop folder ( 'PS_ROOT/override/controller/front/ProductController.php')
If it doesn't work, make sure the class_index.php file has this written in it:
'ProductController' => array ( 'path' => 'override/controllers/front/ProductController.php', 'type' => 'class', 'override' => false, ),or if you want to maintain the source code of your override file in the folder of your module, go like this:
'ProductController' => array ( 'path' => 'modules/NAMEOFYOURMODULE/override/controllers/front/ProductController.php', 'type' => 'class', 'override' => true, ),Again, you just need to hack around in the cache file, if it didn't work from the beginning (which it never has for me though, so I've always had to do it )
4.) Start using your new hook in all your modules! 😄
So now you can use this great new hook to change the template file of a product.
Your hook function will need to RETURN the path to the new template you want to use, and your template file needs to exist. Otherwise it'll just show the default template file.
public function hookActionLoadProductTemplate($params) { $this->product = $params['product']; $this->category = $params['category']; //the product and category arrays are passed along by the product controller. $this->customer = $this->context->customer; //do some custom logic $myrule = TRUE; //for example what type of product you have, and if you customer has purchased it, or what category it is in... //assign any custom variables to your custom template $this->context->smarty->assign('custom stuff', 'custom things go here'); //return the path of your new template. if($myrule) { $this->context->smarty->assign('custom_variable', 'custom value!'); return _PS_MODULE_DIR_.'YOURMODULENAME/views/templates/product_custom_no1.tpl'; } else { $this->context->smarty->assign('another_variable', 'different value!'); return _PS_THEME_DIR_.'product_custom_no2.tpl'; } }
Where do we place hookActionLoadProductTemplate( ) ?
Inside mymodule.php or inside the ProductController.php?
Thanks a lot for your help!