Jump to content

display category meta title in H1 header on product's page


Recommended Posts

I'm struggling with a seemingly simple thing of displaying a category meta_title inside product's H1 header to which the product is bound in prestashop v1.7.4.
If such category doesn't have meta title field set, then showing category name would be a fallback.

I thought implementation is as simple as it sounds, and all one has to use are these two variables:


within a conditional statement, checking of course if meta title isset.
It's true for products bound to only 1 category.
The problem arises when a product is bound to multiple categories with "home" category among them.
Especially when "home" is set as default, making the use of $product.id_category_default completely useless.

The more, above mentioned vars show category name or meta title depending on the place from which visitor enters the product page. And this complicates getting the category data pretty much. I won't go into details not to make this topic too long.

I thought of grabbing all categories to which the product is bound, then checking what sits in fetched array and just display what I need. No matter which category, let it be second or even last one as "home" category is always the first in array.
One of my examples for the last category that is bound to product:

{foreach from=Product::getProductCategories($product.id) item=catitem name=catname}
{if $smarty.foreach.catname.last}

but getProductCategories method gets only id_category, name, link_rewrite and id_lang. No matter the method I use - for example Category::getCategoryInformations - I cannot find a way to fetch whole category data, and get into meta data fields, even using category's ID.

Does somebody know a way of getting whole category meta data from those bound to a product?
Multilang solution isn't needed, but would be appreciated.
I'm not even sure if it is possible to do purely in theme templates without changing core files nor using overrides.

Share this post

Link to post
Share on other sites

I'll answer myself as I doubt anyone will propose anything.
I sticked to my first idea of getting all categories to which the product is bound if the main category is set to "home", and showing the last category info from the array as at last I found a way of getting needed data.

The most important snippet to get category name:

{assign var='ProductLastCatName' value=Category::getCategoryInformations(array(Product::getProductCategories($product.id)|@end))}

Better such solution, than none.

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

  • Create New...

Important Information

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