Jump to content

[solved] How to get categoryID in header while in product page


 Share

Recommended Posts

Hi,

 

I'm having some problems to get category ID in header.tpl while in product page.

 

Here is the code i put in header.php

 

(inspired from http://www.prestasho...name-in-header/ , thanks to his author)

 

class FrontController extends FrontControllerCore
{
public function displayHeader()
{
 $pathinfo = pathinfo(__FILE__);
 $page_name = basename($_SERVER['PHP_SELF'], '.'.$pathinfo['extension']);
 $page_name = (preg_match('/^[0-9]/', $page_name)) ? 'page_'.$page_name : $page_name;
 if ($page_name == 'product' AND isset($_GET['id_product']))
  $product = new Product($_GET['id_product'], false, intval(self::$cookie->id_lang));

  if (Validate::isLoadedObject($product))
self::$smarty->assign('id_category_default', $product->id_category_default);
 }
 parent::displayHeader();
}
}

 

and the code in header.tpl

 

{if isset($id_category_default)}{$id_category_default}{else}{l s='Something else'}{/if}

 

Anybody has an idea why it doesn't work ? My prestashop version is 1.4.4.1

 

Thank you very much

 

efr

Share this post


Link to post
Share on other sites

Solved !

My fault, i made a mistake placing the code given by rocky in header.php, and not in classes/FrontController.php

 

thank you for reading, and thanks to rocky for its code !

Share this post


Link to post
Share on other sites

  • 1 year later...

Hi,

 

I'm having some problems to get category ID in header.tpl while in product page.

 

Here is the code i put header.php

 

(inspired from http://www.prestasho...name-in-header/ , thanks to his author)

 

class FrontController extends FrontControllerCore
{
public function displayHeader()
{
 $pathinfo = pathinfo(__FILE__);
 $page_name = basename($_SERVER['PHP_SELF'], '.'.$pathinfo['extension']);
 $page_name = (preg_match('/^[0-9]/', $page_name)) ? 'page_'.$page_name : $page_name;
 if ($page_name == 'product' AND isset($_GET['id_product']))
  $product = new Product($_GET['id_product'], false, intval(self::$cookie->id_lang));

  if (Validate::isLoadedObject($product))
self::$smarty->assign('id_category_default', $product->id_category_default);
 }
 parent::displayHeader();
}
}

 

and the code in header.tpl

 

{if isset($id_category_default)}{$id_category_default}{else}{l s='Something else'}{/if}

 

Anybody has an idea why it doesn't work ? My prestashop version is 1.4.4.1

 

Thank you very much

 

efr

 

Hi, i want to show canonical link in header in header.tpl

 

 <!-- 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}

 

 

But {$product->id_category_default} is not accesible from header.tpl

Edited by jlmora (see edit history)

Share this post


Link to post
Share on other sites

Hi, i want to show canonical link in header in header.tpl

 

 <!-- 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}

 

 

But {$product->id_category_default} is not accesible from header.tpl

Share this post


Link to post
Share on other sites

  • 3 years later...

Hello,

 

I need to have the category id also in the header, I would like to change the header logo if the customer visit 1 specific category.

Or is there another way to do this?

 

Please can you explain where to put what code.

I'm using Prestashop 1.6.1.4

 

Thank you very much

Kind regards

Peter

Share this post


Link to post
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
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More