Jump to content

[solved] Display products default category name on product list


Coluccini
 Share

Recommended Posts

Hi,

I thought that this would be simple, but I've already spent 3 hours trying to find the answer without luck!

I want to display the default category name of each product listed in product-list.tpl. I've followed this example from TWDesign, but it only works at the product page (and it seems that is not showing default category but the top category). Can you at least help me to modify that example so it works on product-list as well?

Thanks!

Share this post


Link to post
Share on other sites

Well, I found a solution (although I'm not sure if it is the best solution):

 

On controllers/front/CategoryController.php I've added the next two lines inside the foreach from assignProductList()

 

 

$cat = new Category($product['id_category_default'], $this->context->language->id);
$product["category_defaul_name"] = $cat->name;

 

This way you can user $product.category_defaul_name on product-list.tpl

  • Like 1

Share this post


Link to post
Share on other sites

Well, it seems that the default category name it was always there <_< (I'm not sure how I miss it the thousand times I check the product array content)

 

You can use this to print it out

 

{$product.category_default}

Share this post


Link to post
Share on other sites

It shows the ID at the product page, but if you look the getProducts() function at Classes/Category.php the SQL query has cl.`name` AS category_default on the SELECT clause, where cl is category_lang.

If you're using layered navigation, you have to add that and the LEFT JOIN into the module's SQL query or you'll loose the category after filtering.

Share this post


Link to post
Share on other sites

  • 10 months later...

I marked this thread as solved

 

thanks for your solution and suggestions

 

i really appreciate it

 

regards

Hi Vekia,

 

Can you give me the editions to be made on files? I cannot understand what Coluccini posted.

 

Thanks in advance.

Share this post


Link to post
Share on other sites

  • 1 year later...

Hello. I'm reopening post because it's about the same case as mine. In prestashop 1.6.0.6 on product-list I have access to $product['category'] value. It's the category name but without polish accents. Is it a bug or I'm trying to use it improperly?

 

From what I tested category_default displays category name with accents on homepage but on category page it's not defined at all.

Edited by sitte (see edit history)

Share this post


Link to post
Share on other sites

Ok. I found the way if somebody needs it. Tested on Prestashop 1.6.0.6

 

1. Overrride FrontController:

Create new file: /override/classes/controller/FrontController.php with content:

<?php
class FrontController extends FrontControllerCore
{
	public function init()
	{	parent::init();
                $kategorie=Category::getRootCategory()->getSimpleCategories(1); //1 is default language		
		foreach ($kategorie AS $kategoria) $ListaKategorii[$kategoria[id_category]]=$kategoria['name'];	
		self::$smarty->assign('kategorie', $ListaKategorii);
	}
}

2. make frontController override work.

I don't know why it's necessary. I thought it should work without this but:

change /cache/class_index.php into this:

'FrontController' => 
  array (
    'path' => 'override/classes/controller/FrontController.php', 
    'type' => 'class',
  ),

3. Use code: {$kategorie[$product['id_category_default']]} as default kategory name in product-list.tpl foreach function. It works for category and home pages and with filtered module.

Edited by sitte (see edit history)
  • Like 2

Share this post


Link to post
Share on other sites

  • 3 weeks later...

Thanks site! I was able to modify the file with your code and make it work, the only problem is that I am using 2 languages in my store, and this code only uses one language, the default (1).

How could I make it work in 2 languages? So it loads the category name in Spanish and English.

 

Thanks!

-----

 

SOLVED:

change the language ID (1)

to:

($this->context->language->id)

 

-----

 

Is it possible to use the same code: {$kategorie[$product['id_category_default']]} in the product.tpl title?

right now it breaks the website, what do I nee dto modify to make it work in product pages?

 

Thanks!

Edited by mlarino (see edit history)
  • Like 1

Share this post


Link to post
Share on other sites

  • 3 months later...
  • 2 weeks later...

It is also possible do do it without core files modifications

{if isset($product.id_category_default)}
{assign var='catname' value=Category::getCategoryInformations(array($product.id_category_default))}
{$catname[$product.id_category_default].name}
{/if}
  • Like 4

Share this post


Link to post
Share on other sites

  • 3 weeks later...

Are there simplified instructions for this not it's been solved?

 

I have tried adding 

 

{if isset($product.id_category_default)}
{assign var='catname' value=Category::getCategoryInformations(array($product.id_category_default))}
{$catname[$product.id_category_default].name}
{/if}

 

I just end up with:

Fatal error: Cannot use object of type Product as array in /var/www/vhosts/MYSITE/httpdocs/tools/smarty/sysplugins/smarty_internal_templatebase.php(171) : eval()'d code on line 344

Share this post


Link to post
Share on other sites

  • 6 months later...

Hi people, 

 

please why do I get the category friendly-url-alias instead of its name when I paste this following code into product.tpl ?

<a href="{$link->getCategoryLink($product->id_category_default,$product->category)}" title="{$product->category}">{$product->category}</a>

I'm actually struggling with displaying a product's parent category name. This is so sad.
 

Edited by kehezen (see edit history)

Share this post


Link to post
Share on other sites

hello

from your code i can read that $product.category variable contains string that is "seo" friendly, without specials characters, spaces etc?

or is it that ? 

 

In my case, the category name is "Hélène Derrida"

the friendly URL alias is "helene-derrida"

 

and what is output is "helene-derrida".

 

So what's going on ? Is there really no EASY way to fetch this ? If don't wanna sound mad, but actually I am. This is such a waste of time.

Edited by kehezen (see edit history)

Share this post


Link to post
Share on other sites

  • 3 weeks later...

Hi people, 

 

please why do I get the category friendly-url-alias instead of its name when I paste this following code into product.tpl ?

<a href="{$link->getCategoryLink($product->id_category_default,$product->category)}" title="{$product->category}">{$product->category}</a>

I'm actually struggling with displaying a product's parent category name. This is so sad.

 

 

I had this exact same problem. It turns out that in prestashop 1.6 we have the `$category` object available in `product.tpl`. Therefore your example will be:

<a href="{$link->getCategoryLink($category)}" title="{$category->name}">{$category->name}</a>
Edited by Iazel (see edit history)

Share this post


Link to post
Share on other sites

  • 3 months later...

Hi people,

 

  i assign product for root category and subcategory, i go to front office when i click root category after display all products for product listing page,how to display product for sub category wise in product listing page?

 

for example,

Womem->root category,

 

tops->subcategoryname,

 

display tops category products,

 

Dresses->subcategory name,

 

display Dresses category products

Share this post


Link to post
Share on other sites

  • 9 months later...
  • 3 months later...

Hello,

I am using this code

{if isset($product.id_category_default)}
{assign var='catname' value=Category::getCategoryInformations(array($product.id_category_default))}
{$catname[$product.id_category_default].name}
{/if}

That works perfectly but what is the link to the default category ?
Thank you.
regards

  • Thanks 1

Share this post


Link to post
Share on other sites

  • 3 months later...
On 29/8/2015 at 6:31 PM, iqit-commerce said:

It is also possible do do it without core files modifications

{if isset($product.id_category_default)}
{assign var='catname' value=Category::getCategoryInformations(array($product.id_category_default))}
{$catname[$product.id_category_default].name}
{/if}

This works, perfectly, but anybody knows how to show the category image instead the name?

Thanks

Share this post


Link to post
Share on other sites

  • 1 month later...
  • 2 years later...
On 12/13/2017 at 10:10 PM, gothw said:

This works, perfectly, but anybody knows how to show the category image instead the name?

Thanks

 

On 12/13/2017 at 10:10 PM, gothw said:

This works, perfectly, but anybody knows how to show the category image instead the name?

Thanks

where in put this code. i am in catalog/listing/category.tpl put thi code but not any chenges in my product page..
please give me better solustion..?

thank You

 

Share this post


Link to post
Share on other sites

  • 6 months later...
  • 4 weeks 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