Jump to content

Adding category class ID to product page body


maximus33

Recommended Posts

Hi,

 

i have managed to get a category class in all my category pages within the body class, but I can't get the parent category id of my products to be dispalyed in the body class of product pages.

 

Please see my code I have in my header.tpl file below. How can i get the category id in hte product pages too?

 

<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}
category{$product->id_category_default}"{elseif $default_product} class="product{$default_product}"{/if}>

 

as you will see i have tried

class="product{$smarty.get.id_product} category{$product->id_category_default}"

 

to obtian the category id for the product page body class, the product class id is working.

 

Any hlep much appreciated. i am using prestashop version 1.4.8.2

Edited by maximus33 (see edit history)
Link to comment
Share on other sites

Hi,

In productController (or an override) near

self::$smarty->assign('virtual', ProductDownload::getIdFromIdProduct((int)$this->product->id));

You can add

self::$smarty->assign('id_category_everywhere',$id_category);

The in header.tpl

 {IF isset($id_category_every_where)} blah blah cat={$id_category_everywhere}{/If}

 

You will have to get category for the product but as a product can have a default category and/or several others, you have to make a choice.

 

Good dev

Edited by math_php (see edit history)
Link to comment
Share on other sites

The in header.tpl

 {IF isset($id_category_every_where)} blah blah cat={$id_category_everywhere}{/If}

 

You will have to get category for the product but as a product can have a default category and/or several others, you have to make a choice.

 

Hi Thank you, sorry but where in hte header.tpl do I put this in relation to my current code, and what goes where blah blah is ? Sorry if I am been a little slow here. And when you say you have to make a choice how is this done ?

 

thank you again

Link to comment
Share on other sites

Hi sorry I really missed the point.

 

Modify ProductController this way (function preProcess)

$this->product = new Product($id_product, true, self::$cookie->id_lang);
				    self::$smarty->assign('product_id_category',$this->product->id_category_default);

 

And then in your TPL

class="product{$smarty.get.id_product} category{if isset($product->id_category_default)}{$product->id_category_default}{/if}{if isset($product_id_category)}{$product_id_category}{/if}"

This one work. The smarty assign must be done in the preprocess because in process it is to late, body is already rendered.

 

Have a good day

Link to comment
Share on other sites

Thanks again. I have added the first bit of code to this part of the product controller

 

public function preProcess()
{
 global $cart;
 if ($id_product = (int)Tools::getValue('id_product'))
  $this->product = new Product($id_product, true, self::$cookie->id_lang);
									    self::$smarty->assign('product_id_category',$this->product->id_category_default);
 if (Tools::isSubmit('ajax'))
 {
  if (Tools::isSubmit('submitCustomizedDatas'))
  {

 

Thsi is around line 73. Is this right? I have then tried your code for the header tpl like so.

 

<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="product{$smarty.get.id_product} category{if isset($product->id_category_default)}{$product->id_category_default}{/if}{if isset($product_id_category)}{$product_id_category}{/if}">

 

But now I get a blank page, I think this is down to the TPL code as when i change this back the page loads. Where am I going wrong?

Link to comment
Share on other sites

Ok for the controller but in the tpl...

 

Could you please give the code that work for categories and I will add what it need for the product page.

 

The first code you provided

<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}
category{$product->id_category_default}"{elseif $default_product} class="product{$default_product}"{/if}>

Shows multiple errors in tpl rendering, did you put smarty in debug mode ?

Edited by math_php (see edit history)
Link to comment
Share on other sites

Hi

I removed all smarty warning and you have this working code

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

 

You have to put this on one line, else closing of body tag will not be good.

In product page you get the default id_category for the product.

  • Like 1
Link to comment
Share on other sites

  • 4 months later...

Hi

 

I need to put the products canonical link pointing to categories url:

 

<!-- canonical -->

{if $page_name == 'index' or $page_name == 'search'}
 <link rel="canonical" href="{$base_dir}" />
{elseif $page_name == 'category' or $page_name == 'best-sales' or $page_name == 'cart' or $page_name == 'discount' or $page_name == 'manufacturer' or $page_name == 'new-products' or $page_name == 'prices-drop'}
 <link rel="canonical" href="{$base_dir}{$request_uri|substr:1|regex_replace:'/\/(.*)/':''|regex_replace:'/\?(.*)/':''}" />
{else}
  <link rel="canonical" href="{$base_dir}{$product->id_category_default}-{$request_uri|substr:1|regex_replace:'/\/(.*)/':''|regex_replace:'/\?(.*)/':''}" />
{/if}

 

This works "fine" but {$product->id_category_default} is not accesible from header.tpl, and my canonicals link needs the id_category

 

¿Could you help me?

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