Show product's category name in product listing

  • Posts: 28
  • Thank you received: 0
6 years 8 months ago #275953

Hi,

I would like to show each product's category name in the product listing page. What is the code that displays the category name?

Thanks!

Please Log in or Create an account to join the conversation.

  • Posts: 26000
  • Thank you received: 4004
  • MODERATOR
6 years 8 months ago #275973

Hello,

When you create a post in our forum ; please fill the requested elements so our support team will have the required knowledge to be able to answer you with more accuracy.
Thank you for your understanding.

About your request, without knowing your website, the context and the HikaShop version number you're using ; I'm afraid that I can't answer you.

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

Please Log in or Create an account to join the conversation.

  • Posts: 12953
  • Thank you received: 1778
6 years 7 months ago #276466

Hello,

I would like to show each product's category name in the product listing page. What is the code that displays the category name?


I think that you created a new thread to give us more information about what you wanted to do but I erased it by mistake, my bad.

There is no option to display the categories of a product on the listing since usually you have only one category and a listing of products already bases itself on that category to display the products listing so you already know the category of all the products of the listing and they usually are the same.

So you'll indeed need to edit the view file which displays each product in order to load its categories and then display them.
First, to know which view file you need to edit and how you can edit it, I would recommend that you read this:
www.hikashop.com/support/documentation/1...-display.html#layout
After that, if you're a developer, you should easily find how to get the product_id of the current product, run a MySQL query to load the categories of the product from the database and display that.

Please Log in or Create an account to join the conversation.

  • Posts: 28
  • Thank you received: 0
6 years 7 months ago #276537

Hi,

Thanks for getting back. Previously I enquired on how to display branding of the product at the listing page and was told to add some codes to the listing_img_title view. ( here ) Would it be as simple as that? (Are there pre-written codes that I can just copy and paste to the view file)

Unfortunately, I'm not a developer and do not know how to run a MySQL query or write the codes, they are too complex for me. Would you be able to point me in the right direction? If not, do you do paid customization?

Please Log in or Create an account to join the conversation.

  • Posts: 1119
  • Thank you received: 114
6 years 7 months ago #276592

Hi,

This should do the job:

<div class="clearfix related-categories-list">
<?php
$q = 'SELECT c.* '.
	' FROM ' . hikashop_table('product_category').' as pc '.
	' INNER JOIN '.hikashop_table('category').' AS c ON pc.category_id = c.category_id '.
	' WHERE pc.product_id = '.$this->row->product_id;
$database->setQuery($q);
$product_categories = $database->loadObjectList();
$categories = array();
if($product_categories) foreach ($product_categories as $category) $categories[] = $category->category_name;
if(count($categories) > 2) $categories = array_splice($categories, 0, 2);
foreach($categories as $k=>$c){if($k==count($categories)-1) echo '<span class="pull-left margin-reset">'.$c.'</span>'; else echo '<span class="pull-left margin-reset">'.$c.',</span>';}
?>
</div>

This will display max 2 categories of the product.

If your product is in more then 2 categories then you will have to modify numbers in this line:
if(count($categories) > 2) $categories = array_splice($categories, 0, 2);

You can add code above based on product view you are using. As example product/listing_img_title

Hope that helps

Kind Regards

Last edit: 6 years 7 months ago by kyratn.
The following user(s) said Thank You: Philip

Please Log in or Create an account to join the conversation.

  • Posts: 81480
  • Thank you received: 13062
  • MODERATOR
6 years 7 months ago #276549

Hi,

Well, there is no "pre written" code but the code isn't that complex to write. It will require loading the categories of the product and then echoing it in the view file.
A 3rd party developer should be able to add that for you for a small price if necessary.
You can for example contact our partners:
www.hikashop.com/home/our-partners.html

Please Log in or Create an account to join the conversation.

Time to create page: 0.073 seconds
Powered by Kunena Forum