Display of category name in the checkout cart

  • Posts: 36
  • Thank you received: 5
  • Hikashop Essential
3 days 10 hours ago #364030

-- HikaShop version -- : 5.1.1
-- Joomla version -- : 5.1.4
-- PHP version -- : 8.2
-- Browser(s) name and version -- : Firefox

Hello,
I would like to be able to display the category name in the cart. Is there an easy way to do that?
Thank you!

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

  • Posts: 82680
  • Thank you received: 13330
  • MODERATOR
3 days 7 hours ago #364034

Hi,

You'll have to edit the view file checkout / show_block_cart.php via the Display>Views menu.
There, the product name is displayed with the line:

echo $product->product_name;
You could add after it something like that for example:
$productClass = hikashop_get('class.product');
$categories = $productClass->getCategories($product->product_id);
$firstCategory = reset($categories);
echo ' ( '.$firstCategory->category_name.' )'; 

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

  • Posts: 36
  • Thank you received: 5
  • Hikashop Essential
1 day 12 hours ago #364094

Hi Nicolas,

I've added the code you've mentioned before the "echo $product->product_name;" line; however, it's appearing as two parenthesis before the product name like that: () (see attached image)

Do you know what I could change to have the category name appear correctly? Also, if it's possible, I'd like to have it in bold.

Thank you!

Attachments:

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

  • Posts: 82680
  • Thank you received: 13330
  • MODERATOR
1 day 9 hours ago #364096

Hi,

My bad. You should do it like this:

$productClass = hikashop_get('class.product');
$categories = $productClass->getCategories($product->product_id);
$firstCategoryId = reset($categories);
$categoryClass = hikashop_get('class.category');
$category = $categoryClass->get($firstCategoryId);
echo ' <b>( '.$category->category_name.' )</b>'; 

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

  • Posts: 36
  • Thank you received: 5
  • Hikashop Essential
1 day 6 hours ago #364103

Hi,

Thank you. It's now working with one of my products, but it doesn't appear on the other products. I'm not sure what I did different for that product. I've tried other products from the same category, but it's the only one for which the category name appears on the checkout cart. Do you know what could be causing the problem with the other products? I've attached an image to show you how it appears. Also, I removed the parenthesis from the last line of the code you gave me as otherwise the parenthesis was showing up on the cart. Here is what I entered instead:

echo ' <b> '.$category->category_name.' - </b>';

Not sure if that could be causing the issue, but i've tried with the parenthesis and it didn't change anything (apart from the parenthesis appearing on both sides of the category name). The category name still only appeared with one product.

Also I've realised that my categories are in plural ("orginal artworks" and "fine art prints") and I'd prefer them to appear as "original artwork" and "fine art print" (without the "s") on the checkout cart if possible. Is there a way to do that? Maybe with a custom field?

Thank you!

Attachments:

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

  • Posts: 82680
  • Thank you received: 13330
  • MODERATOR
17 hours 15 minutes ago #364107

Hi,

That's because the product added to the cart is actually not a product but a variant. And variants are not directly linked to products.
So instead of :

$productClass = hikashop_get('class.product');
$categories = $productClass->getCategories($product->product_id);
you should use:
$id = $product->product_id;
if(!empty($product->product_parent_id))
  $id = $product->product_parent_id;
$productClass = hikashop_get('class.product');
$categories = $productClass->getCategories($id);

And yes, you could use a custom field of the table "category" and of the type "text" to be able to provide the name to be displayed in the cart.
And in the last line, you can then change category_name to the column name of your custom field.

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

Time to create page: 0.067 seconds
Powered by Kunena Forum