elpoleto Posted April 2, 2012 Share Posted April 2, 2012 Hello Is there a way to have a different product page template for a specific category (and its subs) ? Thanks Marco Link to comment Share on other sites More sharing options...
Eihwaz Posted April 2, 2012 Share Posted April 2, 2012 (edited) I would do something like this (warning: haven't tested): create a file in your /overrides/controllers/ called ProductController.php with the following content: <?php class ProductController extends ProductControllerCore { private static $_product_category = false; public function process() { parent::process(); $method = method_exists(self::$smarty, 'get_template_vars') ? 'get_template_vars' : method_exists(self::$smarty, 'getTemplateVars') ? 'getTemplateVars' : false; if ($method) { $category = call_user_func(array(self::$smarty, $method), 'category'); if (is_object($category) && Validate::isLoadedObject($category)) { self::$_product_category = $category; } } } private static function checkProductTemplate($link_rewrite) { return file_exists(_PS_THEME_DIR_ . 'product_' . $link_rewrite . '.tpl'); } public function displayContent() { FrontController::displayContent(); $template = 'product.tpl'; if (self::$_product_category && Validate::isLoadedObject(self::$_product_category)) { // Check if child category product template exists if ( ! Tools::isEmpty(self::$_product_category->link_rewrite) && self::checkProductTemplate(self::$_product_category->link_rewrite)) { $template = 'product_' . self::$_product_category->link_rewrite . '.tpl'; } else { // If not, check if parent's category producttemplate exists $parents = self::$_product_category->getParentsCategories(); if (is_array($parents) && sizeof($parents) > 1) { $parent_category = array_pop($parents); if ( ! Tools::isEmpty($parent_category['link_rewrite']) && self::checkProductTemplate($parent_category['link_rewrite'])) { $template = 'product_' . $parent_category['link_rewrite'] . '.tpl'; } } } } self::$smarty->display(_PS_THEME_DIR_ . $template); } } Now you can simply create different product templates, just add the category's SEO url to the filename, like so: product_music-ipods.tpl And put it in your theme templates directory, next to the original product.tpl. This should work for both - categories and subcategories. Edited April 12, 2012 by Eihwaz (see edit history) 2 Link to comment Share on other sites More sharing options...
Svanhoof Posted April 11, 2012 Share Posted April 11, 2012 Hi, Your solution would be the perfect answer to my question. I've tested it immediately... but there is a small problem, I think. I was looking for a way to show the "special text field area" in only one category with all his subcategories. I did exactly what you suggested but I see this: http://www.kanvaz.be/product.php?id_product=38 You can see the product twice. The first product is the correct one (with the correct translations) but without the text area and without the left column. The second product is correct but with some english phrases. And with the correct text area field. At the top you see the same two rows of breadcrumbs. I've called my new .TPL file : product_giraf.tpl (=subcategory) When I do this for the whole category, nothing happened except there is no left column. (product_kinderkamer.tpl) It would be nice if you have a solution for this problem. Because I'm looking for this issue for so long :-) And I'm pretty close to the solution :-) Thanx in advance, Sofie Link to comment Share on other sites More sharing options...
Eihwaz Posted April 12, 2012 Share Posted April 12, 2012 Hi Sofie, Try the updated code in my first post. As for the English phrases - unfortunately you'll have to translate them again in your Tools -> Translations -> Front Office Translations, because Prestashop takes template name into account, so you do have translations for your product.tpl file, but not for your product_giraf.tpl file. Other than that, this code should work fine now. Cheers! Link to comment Share on other sites More sharing options...
Svanhoof Posted April 12, 2012 Share Posted April 12, 2012 WOW!! This is perfect! It works very well. Thank you very much for your time and your code :-) Sofie Link to comment Share on other sites More sharing options...
noesac Posted April 26, 2012 Share Posted April 26, 2012 Doh, looks like this doesn't work for 1.3:( Link to comment Share on other sites More sharing options...
aiteampl Posted June 6, 2012 Share Posted June 6, 2012 Hi, tried everything as you described and no results. any idea, tips? using PS 1.4.8.2 Link to comment Share on other sites More sharing options...
lain Posted August 28, 2012 Share Posted August 28, 2012 thanks for the code, working on 1.4.8.2 Link to comment Share on other sites More sharing options...
Gnom1k Posted November 9, 2012 Share Posted November 9, 2012 Many thanks! Can I do the same for global.css? I want to get something like this: I have a template for main page with it's own design, and categories like "green goods" and "red goods". When visitor enters a product page of "green goods" (and category page) system use global_green-goods.css instead of global.css. Same goes for red goods. If anyone will give me the way to solve it, I will make an easy guide for novices like me. Make love, not war Link to comment Share on other sites More sharing options...
S7 Media Ltd Posted March 9, 2013 Share Posted March 9, 2013 This looks like it may be something I need. Thank you! Link to comment Share on other sites More sharing options...
rene123456 Posted December 20, 2013 Share Posted December 20, 2013 Anyone know of any module that does this for category (category.tpl) and can be configured from Backoffice. Thank you! Link to comment Share on other sites More sharing options...
NBBO Posted February 20, 2014 Share Posted February 20, 2014 Anyone that can help me get this to work with 1.5.4.1? I need this override for two website projects, but it does not seem to work with prestashop v. 1.5.4. I tried deleting the class_index file in cache, disable cache, but it does not accept the new template, just keeps using the old. I´ve added the productcontroller.php, added product_stars.tpl (stars being my friendly url for the category) nothing.. Link to comment Share on other sites More sharing options...
vekia Posted February 21, 2014 Share Posted February 21, 2014 you can do it in productcontroller inside initcontent function. there is a code: $this->context->smarty->assign('errors', $this->errors); $this->setTemplate(_PS_THEME_DIR_.'product.tpl'); you can add there with if condition with $product->id_category_default and depending on if condition results - use different template file Link to comment Share on other sites More sharing options...
NBBO Posted February 21, 2014 Share Posted February 21, 2014 Thanks for the fast response Vekia. I think i follow what you´re saying, but just to be sure.. Instead of making an override file, you want me to simply put an if condition in the existing productcontroller? Can i use some of the code above, but with $product->id_category_default instead of the above $_product_category? Link to comment Share on other sites More sharing options...
vekia Posted February 22, 2014 Share Posted February 22, 2014 sorry it was my mistake, instead of $product you have to use $this->product this is difference between prestashop 1.4 and 1.5 Link to comment Share on other sites More sharing options...
NBBO Posted March 3, 2014 Share Posted March 3, 2014 (edited) I managed to resolve my issue with fragments of the different things u´ve told me, and a lil read on smarty.net and forums. Meanwhile i learned a little smarty and php, and also understod another part of the puzzle that is prestashop^^ I got the product comments to show for only category with a specific id, In controllers/front/productcontroller.php about line 248 inside the; $this->context->smarty->assign(array( Write: 'category' => $this->category, 'id_category' => (int)$this->category->id, 'id_category_parent' => (int)$this->category->id_parent, After that it was just putting everything in tab.tpl and the first "idtab5" part of productcomments.tpl, into a if statement using the context defined above... {if $category->id == 7} {/if} if u wanna add more ids; {if $category->id == 7 AND $category->id == 3} {/if} I´m pretty sure i can use this, to fix this topics original problem for 1.5.4. To use different product templates for different categories. Since i need this fix for my other website, i will look at it during the month, and post the solution in this topic. Edited March 3, 2014 by NBBO (see edit history) Link to comment Share on other sites More sharing options...
NBBO Posted March 9, 2014 Share Posted March 9, 2014 (edited) Hey Everyone. Sorry for not posting the solution, but i found a way around it, and can solve the rest of the small problems with the solution above. I´m sure most of you can do the same, or at least find a solution that fits. Currently working on a PS 1.6 update of the site, so i won´t spend anymore time on this issue. Edited March 25, 2014 by NBBO (see edit history) Link to comment Share on other sites More sharing options...
websitebuilder Posted July 17, 2014 Share Posted July 17, 2014 I have a question on this one. I have my website which I am pleased with but have found some pages I dont now like. Is it possible to pul those pages from another theme. I.e. for the product list (where it shows all the products in a menu) so it loads a different theme for this part and leaves my existing header, footer etc? I presume it would also need to pull over some CSS as well? Link to comment Share on other sites More sharing options...
apatan Posted September 30, 2014 Share Posted September 30, 2014 (edited) Based on this post, I made the following changes in the controllers/front/Productcontroller.php $this->context->smarty->assign('errors', $this->errors); if ($category->id == 25) { $this->setTemplate(_PS_THEME_DIR_.'product1.tpl'); } else { $this->setTemplate(_PS_THEME_DIR_.'product2.tpl'); } Still I am not able to see any changes on the website. Where am I going wrong? Can you help please? I am using version 1.5.6.0 Thanks a lot. Edited September 30, 2014 by apatan (see edit history) Link to comment Share on other sites More sharing options...
paradiserahul Posted June 16, 2016 Share Posted June 16, 2016 hi,i want to show the custom tpl file instead of productlist.tpl page for some categories Link to comment Share on other sites More sharing options...
Rhobur Posted June 18, 2016 Share Posted June 18, 2016 In the category.tpl change the setTemplate function argument from product-list.tpl to product-list-whatever.tpl You find this function at the page bottom. Link to comment Share on other sites More sharing options...
Axl Posted January 31, 2018 Share Posted January 31, 2018 (edited) Very old topic, but I like to shrare my solution for Presta 1.7.2.4, based on the idea of Eihwaz The path has changed to /override/controllers/front, put the file in there or put it in your module/override/controllers/front, before install. Quote create a file in your /overrides/controllers/ called ProductController.php with the following content: ... Now you can simply create different product templates, just add the category's SEO url to the filename, like so: product_music-ipods.tpl And put it in your theme templates directory, next to the original product.tpl. This should work for both - categories and subcategories. <?php class ProductController extends ProductControllerCore { private static function checkProductTemplate($link_rewrite) { return file_exists(_PS_THEME_DIR_ . 'templates/' . $link_rewrite . '.tpl'); } public function init() { parent::init(); if ($this->template == 'catalog/product.tpl') { $id_product = (int) Tools::getValue('id_product'); $tpl = 'catalog/product_' . $this->product->category; if ($this->checkProductTemplate($tpl)) { $this->setTemplate($tpl, array( 'entity' => 'product', 'id' => $id_product, )); } } } } Edited January 31, 2018 by Axl (see edit history) 1 Link to comment Share on other sites More sharing options...
lenti Posted May 23, 2020 Share Posted May 23, 2020 On 1/31/2018 at 11:01 PM, Axl said: Very old topic, but I like to shrare my solution for Presta 1.7.2.4, based on the idea of Eihwaz The path has changed to /override/controllers/front, put the file in there or put it in your module/override/controllers/front, before install. <?php class ProductController extends ProductControllerCore { private static function checkProductTemplate($link_rewrite) { return file_exists(_PS_THEME_DIR_ . 'templates/' . $link_rewrite . '.tpl'); } public function init() { parent::init(); if ($this->template == 'catalog/product.tpl') { $id_product = (int) Tools::getValue('id_product'); $tpl = 'catalog/product_' . $this->product->category; if ($this->checkProductTemplate($tpl)) { $this->setTemplate($tpl, array( 'entity' => 'product', 'id' => $id_product, )); } } } } Thank you for this!!! I was looking for a solution for a couple of hours and failing to apply. Your simple solution/quote of the final product works just fine. PS version 1.7.6.4 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now