Jump to content

[Solved] Different product.css file for each category


Recommended Posts

Hello everybody,

I am about to tear my hair out ... I am trying to create a different stylesheet for each product category. I found multiple solutions to get a distinct global.css file for each category, but this is not really my problem.

In fact, I want to use a different product.css file for each product category.
Example : category 1 -> product-1.css ; category 2 -> product-2.css ; etc.

I don't find the right solution for that. I think I need to custom ProductController.php at this level :

class ProductControllerCore extends FrontController
{
   protected $product;

   public function setMedia()
   {
       parent::setMedia();

       Tools::addCSS(_THEME_CSS_DIR_.'product.css');



… but I don't know how I should modify the last line.

I would go for something like this :

Tools::addCSS(_THEME_CSS_DIR_.'product-{$product->id_category_default}.css');



… but I am quite sure this will not work.

So I am asking for your help !

Thanks in advance.

Best regards,

Link to comment
Share on other sites

What I suggested did not work. I also attempted to call the value of the category to display a given CSS file:

 $id_caty = (int)Tools::getValue('id_category_default');

       if ($id_caty == 5)
       {
       Tools::addCSS(_THEME_CSS_DIR_.'product-5.css');
       }
       else
       {
       Tools::addCSS(_THEME_CSS_DIR_.'product.css');
       }



But it still does not work. Someone could maybe tell me if my code is wrong ?

Thanks in advance for your answer.

Link to comment
Share on other sites

Open header.tpl

Replace:

<body {if $page_name}id="{$page_name|escape:'htmlall':'UTF-8'}"{/if}>



With:

<body {if $page_name}id="{$page_name|escape:'htmlall':'UTF-8'}"{/if}{if $smarty.get.id_category} class="category{$smarty.get.id_category}"{elseif $default_category} class="category{$default_category}"{/if}{if $smarty.get.id_product} class="product{$smarty.get.id_product}"{elseif $default_product} class="product{$default_product}"{/if}>



You will be able to use just the one stylesheet.

This will create a unique class for each category and product....I hope this works for you :)

Link to comment
Share on other sites

Thank you so much taurasmark. It works !

I also changed a few things in addition.

In ProductController.php, I couldn't call the ID of the category, so I called the ID of the product and created a if condition with that :

class ProductControllerCore extends FrontController
{
   protected $product;

   public function setMedia()
   {
       parent::setMedia();

       $id_cvb = (int)Tools::getValue('id_product');

       if ($id_cvb > 9 && $id_cvb < 19)
       {
       Tools::addCSS(_THEME_CSS_DIR_.'product13.css');
       }
       else
       {
       Tools::addCSS(_THEME_CSS_DIR_.'product.css');
       }



Then, I also associated a given product.tpl page to certain products, depending on their IDs. That gives us :

    public function displayContent()
   {
       parent::displayContent();

       $deux = (int)Tools::getValue('id_product');

       if ($deux > 9 && $deux < 19)
       {
       self::$smarty->display(_PS_THEME_DIR_.'product13.tpl');
       }
       else
       {
       self::$smarty->display(_PS_THEME_DIR_.'product.tpl');
       }

   }



So I have two TPL files and two CSS files, with different parameters.

Thanks again, have a good day.

Link to comment
Share on other sites

  • 1 year later...

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...