Jump to content

different product template for a specific category


Recommended Posts

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 by Eihwaz (see edit history)
  • Like 2
Link to comment
Share on other sites

  • 2 weeks later...

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

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

  • 2 weeks later...
  • 1 month later...
  • 2 months later...
  • 2 months later...

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 :D

Link to comment
Share on other sites

  • 4 months later...
  • 9 months later...
  • 1 month later...

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

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

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

  • 2 weeks later...

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 by NBBO (see edit history)
Link to comment
Share on other sites

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 by NBBO (see edit history)
Link to comment
Share on other sites

  • 4 months later...

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

  • 2 months later...

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 by apatan (see edit history)
Link to comment
Share on other sites

  • 1 year later...
  • 1 year later...

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 by Axl (see edit history)
  • Thanks 1
Link to comment
Share on other sites

  • 2 years later...
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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...