Display of category name in the checkout cart

  • Posts: 41
  • Thank you received: 6
  • Hikashop Essential
1 month 56 minutes 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: 82823
  • Thank you received: 13370
  • MODERATOR
4 weeks 2 days 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: 41
  • Thank you received: 6
  • Hikashop Essential
4 weeks 1 day 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: 82823
  • Thank you received: 13370
  • MODERATOR
4 weeks 23 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: 41
  • Thank you received: 6
  • Hikashop Essential
4 weeks 20 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: 82823
  • Thank you received: 13370
  • MODERATOR
4 weeks 7 hours 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.

The following user(s) said Thank You: Noe

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

  • Posts: 41
  • Thank you received: 6
  • Hikashop Essential
2 weeks 2 days ago #364290

Thank you ! It’s now appearing correctly. However, it is not giving me the possibility to translate the content. What shall I add to have the custom field appearing in the translation file available on the category and/ or product page of the backend?

I’ve decided to create custom fields as it allows me to have information on top of the product’s name as well as below it, where I can add any relevant extra information. So I have one category custom field (WYSIWYG) on top of the product’s name and one product custom field (WYSIWYG) below the product’s name.

Also, I would like to add the same custom fields to the invoice (display view: order/show.php), but I’m not sure where to put the code. I have tried to add the same code there in various places, but it’s not working so I guess I’m not putting it at the right place. I would need the custom fields to be translated there also.

Thank you!

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

  • Posts: 82823
  • Thank you received: 13370
  • MODERATOR
2 weeks 1 day ago #364302

Hi,

Instead of directly echoing the value of the custom field in the database, you need to ask the custom field system to display it with its "show" method. You can see an example of this here:
www.hikashop.com/forum/product-category-...t-layout.html#339434


In order / show, the product data from the order is in $product after the line:

foreach($this->order->products as $product) {
However, this variable doesn't contain the product data like on the product page.
The product data like on the product page is in $productData after the line:
$productData = $this->products[ (int)$product->product_id ];
While product_id is available in both, product_parent_id is only available in $productData. So you need to adapt a bit the code.

The following user(s) said Thank You: Noe

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

  • Posts: 41
  • Thank you received: 6
  • Hikashop Essential
1 week 2 hours ago #364462

Hello,

Thank you for your answer but this is a bit too technical for me.

With regards to the translation, I've tried the following code but it is still not displaying the translation when I'm on the French page. Do you know what I could change in order to have the translation display on the page?

$id = $product->product_id;
if(!empty($product->product_parent_id))
  $id = $product->product_parent_id;
$productClass = hikashop_get('class.product');
$categories = $productClass->getCategories($id);
$firstCategoryId = reset($categories);
$categoryClass = hikashop_get('class.category');
$category = $categoryClass->get($firstCategoryId);
$field = $this->fieldsClass->getField('categoryname_forcart', 'category');
echo $this->fieldsClass->show($field, $this->element->categoryname_forcart);

I removed the last line of the code you gave me
echo $category->categoryname_forcart;

and replaced it with:
$field = $this->fieldsClass->getField('categoryname_forcart', 'category');
echo $this->fieldsClass->show($field, $this->element->categoryname_forcart);

I have created 2 custom fields of the type WYSIWYG (one product table and one category table). The code I shared here is the one I used for the category custom field.

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

  • Posts: 82823
  • Thank you received: 13370
  • MODERATOR
6 days 23 hours ago #364463

Hi,

You're close.
On the last line, you're using $this->element->categoryname_forcart but $this->element contains the data of the product. The data of the category is in $category
Change this and it should work.

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

  • Posts: 41
  • Thank you received: 6
  • Hikashop Essential
4 days 2 hours ago #364495

Hi,

It's still not working. Not sure if I didn't understand what you meant or if it's a problem with the code.

Here is what I've entered:

$id = $product->product_id;
if(!empty($product->product_parent_id))
  $id = $product->product_parent_id;
$productClass = hikashop_get('class.product');
$categories = $productClass->getCategories($id);
$firstCategoryId = reset($categories);
$categoryClass = hikashop_get('class.category');
$category = $categoryClass->get($firstCategoryId);
$field = $this->fieldsClass->getField('categoryname_forcart', 'category');
echo $this->fieldsClass->show($field, $category->categoryname_forcart);

It's giving me this error page:

Attachments:

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

  • Posts: 82823
  • Thank you received: 13370
  • MODERATOR
4 days 21 minutes ago #364498

Hi,

This error says that $this->fieldsClass doesn't exist in the view file where you're adding that code.
So you need to add an extra line at the beginning of your code:

$this->fieldsClass = hikashop_get('class.field');

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

Time to create page: 0.082 seconds
Powered by Kunena Forum