Jump to content

[SOLVED] Load category name and product name in header


Recommended Posts

Hi,

I'm facing some problems to load category and product name in header.tpl. Since the smarty arrays that contains this info are loaded after header.tpl, i cant use the {$product->name} or {$category->name} variables in header.tpl. How can i do that? assign this variables to header?

My point is, in header put the category or product name ex.:

{if $page == 'category'}{$category->name}{elseif $page=='product'}{$product->name}{else}{l s='Hi something'}{/if}

How can i do that?

Thanks

Mozack

Share this post


Link to post
Share on other sites

You'll need to add the following code in header.php before the $smarty->display:

if ($page_name == 'category' AND isset($_GET['id_category']))
{
  $category = new Category($_GET['id_category'], intval($cookie->id_lang));

  if (Validate::isLoadedObject($category))
     $smarty->assign('category_name', $category->name);
}
elseif ($page_name == 'product' AND isset($_GET['id_product']))
{
  $product = new Product($_GET['id_product'], false, intval($cookie->id_lang));

  if (Validate::isLoadedObject($product))
     $smarty->assign('product_name', $product->name);
}



then you can add the following to header.tpl:

{if isset($category_name)}{$category_name}{elseif isset($product_name)}{$product_name}{else}{l s='Hi something'}{/if}

  • Like 1

Share this post


Link to post
Share on other sites

Hi,

It works like a charm... I'm sorry but i need one more help. Integrate the cover image of product to load in header as the product name... I was seeing the code but i can not figure it out.

Thanks for all your help,

Regards

Mozack

Share this post


Link to post
Share on other sites

So you want a small product cover image instead of the product name? To do that, you'll need to add the following to header.php:

if ($page_name == 'category' AND isset($_GET['id_category']))
{
   $category = new Category($_GET['id_category'], intval($cookie->id_lang));
   if (Validate::isLoadedObject($category))
       $smarty->assign('category_name', $category->name);
}
elseif ($page_name == 'product' AND isset($_GET['id_product']))
{   
   $product = new Product($_GET['id_product'], false, intval($cookie->id_lang));
   if (Validate::isLoadedObject($product))
       $smarty->assign('product', $product);

   $images = $product->getImages(intval($cookie->id_lang));
   foreach ($images AS $k => $image)
       if ($image['cover'])
       {
           $cover = $image;
           $cover['id_image'] = intval($product->id).'-'.$cover['id_image'];
           $cover['id_image_only'] = intval($image['id_image']);
       }
   if (!isset($cover))
       $cover = array('id_image' => Language::getIsoById($cookie->id_lang).'-default', 'legend' => 'No picture', 'title' => 'No picture');
   $smarty->assign('cover', $cover);
}



then use the following in header.tpl:

{if isset($category_name)}{$category_name}{elseif isset($product_name)}<img src="{$link->getImageLink($product->link_rewrite, $cover.id_image, 'small')}" title="{$product->name|escape:'htmlall':'UTF-8'}" alt="{$product->name|escape:'htmlall':'UTF-8'}" />{else}{l s='Hi something'}{/if}

Share this post


Link to post
Share on other sites

Hi,

I've corrected the code, and now, the code is:

if ($page_name == 'category' AND isset($_GET['id_category']))
{
   $category = new Category($_GET['id_category'], intval($cookie->id_lang));
   if (Validate::isLoadedObject($category))
       $smarty->assign('category_name', $category->name);
}
elseif ($page_name == 'product' AND isset($_GET['id_product']))
{   
   $product = new Product($_GET['id_product'], false, intval($cookie->id_lang));
   if (Validate::isLoadedObject($product))
       $smarty->assign('product_name', $product->name);


   $images = $product->getImages(intval($cookie->id_lang));
   foreach ($images AS $k => $image)
       if ($image['cover'])
       {
           $cover = $image;
           $cover['id_image'] = intval($product->id).'-'.$cover['id_image'];
           $cover['id_image_only'] = intval($image['id_image']);
       }
   if (!isset($cover))
       $cover = array('id_image' => Language::getIsoById($cookie->id_lang).'-default', 'legend' => 'No picture', 'title' => 'No picture');
       $smarty->assign('cover', $cover);

}



I think the problem was one }

Thanks for your help, everything is right now...

Regards

Share this post


Link to post
Share on other sites

  • 1 year later...
  • 3 weeks later...

sorry, i put in override/controllers instead of override/classes (confusing because of name frontcontroller)

now it works.

but if you want to get the cover image, you need to add code from post above (and add self:: before smarty assign)

 

so after:

  if (Validate::isLoadedObject($product))
   self::$smarty->assign('product_name', $product->name);

 

place:

  $images = $product->getImages(intval($cookie->id_lang));
  foreach ($images AS $k => $image) {
   if ($image['cover']) {
 $cover = $image;
 $cover['id_image'] = intval($product->id).'-'.$cover['id_image'];
 $cover['id_image_only'] = intval($image['id_image']);
   }
  }
  if (!isset($cover)) $cover = array('id_image' => Language::getIsoById($cookie->id_lang).'-default', 'legend' => 'No picture', 'title' => 'No picture');
  self::$smarty->assign('cover', $cover);

 

and in header.tpl:

 {if isset($product_name)}<meta property="og:image" content="{$link->getImageLink($product->link_rewrite, $cover.id_image, 'list')}" />{/if}

it gives you:

<meta property="og:image" content="http://easypeas.be/173-list/.jpg" />

but that is a valid image

 

hope this will help

  • Like 2

Share this post


Link to post
Share on other sites

  • 3 months later...
  • 3 months later...
  • 1 month later...

sorry, i put in override/controllers instead of override/classes (confusing because of name frontcontroller)

now it works.

but if you want to get the cover image, you need to add code from post above (and add self:: before smarty assign)

 

so after:

  if (Validate::isLoadedObject($product))
self::$smarty->assign('product_name', $product->name);

 

place:

  $images = $product->getImages(intval($cookie->id_lang));
  foreach ($images AS $k => $image) {
if ($image['cover']) {
 $cover = $image;
 $cover['id_image'] = intval($product->id).'-'.$cover['id_image'];
 $cover['id_image_only'] = intval($image['id_image']);
}
  }
  if (!isset($cover)) $cover = array('id_image' => Language::getIsoById($cookie->id_lang).'-default', 'legend' => 'No picture', 'title' => 'No picture');
  self::$smarty->assign('cover', $cover);

 

and in header.tpl:

 {if isset($product_name)}<meta property="og:image" content="{$link->getImageLink($product->link_rewrite, $cover.id_image, 'list')}" />{/if}

it gives you:

<meta property="og:image" content="http://easypeas.be/173-list/.jpg" />

but that is a valid image

 

hope this will help

 

Hello. Can you helo me to display category image instead of category title in header ?

Share this post


Link to post
Share on other sites

  • 3 months later...
  • 1 month later...

Hi, i want to show canonical link in header (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

  • 1 month later...

Hi!

 

I'm a little confused. I don't know where exactly to put the PHP code for assigning the smarty variables.

You wrote: "You'll need to add the following code in header.php before the $smarty->display"

My header.php file contains only the following two lines:

$controller = new FrontController();

$controller->displayHeader();

 

The assignment of the variables doesn't work when I put the code anywhere in this file.

 

I'm using PrestaShop™ 1.4.4.1.

 

Thanks for a little help!

Share this post


Link to post
Share on other sites

Hi.

 

I could solve my problem without editing any php file.

 

I just put this code in my header.tpl file:

 

 

{php}
        $product = new Product($_GET['id_product'], false, intval($cookie->id_lang));
$images = $product->getImages(intval($cookie->id_lang));
           foreach ($images AS $k => $image)
               if ($image['cover'])
               {
                   $cover = $image;
                   $cover['id_image'] = intval($product->id).'-'.$cover['id_image'];
                   $cover['id_image_only'] = intval($image['id_image']);
                   echo("<meta property='og:image' content='img/p/".$cover['id_image']."-large.jpg' />");
               }

{/php}

Share this post


Link to post
Share on other sites

  • 1 year later...

Hi Rocky,

 

I know this is an old post but I am still using an old version of prestashop :-(

 

I am looking to do something similar in my header : 

 

{if $page_name == 'product'}{$product->reference|escape:'htmlall':'UTF-8'} {$category->name|escape:'htmlall':'UTF-8'} {else} {l s='Hi something'}{/if}

 

 

Any idea how I should proceed ?

 

Thanks for your help.

 

 

You'll need to add the following code in header.php before the $smarty->display:
 

if ($page_name == 'category' AND isset($_GET['id_category'])){   $category = new Category($_GET['id_category'], intval($cookie->id_lang));   if (Validate::isLoadedObject($category))      $smarty->assign('category_name', $category->name);}elseif ($page_name == 'product' AND isset($_GET['id_product'])){   $product = new Product($_GET['id_product'], false, intval($cookie->id_lang));   if (Validate::isLoadedObject($product))      $smarty->assign('product_name', $product->name);}

then you can add the following to header.tpl:

{if isset($category_name)}{$category_name}{elseif isset($product_name)}{$product_name}{else}{l s='Hi something'}{/if}

Share this post


Link to post
Share on other sites

  • 7 months 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
 Share

×
×
  • Create New...

Important Information

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