Display Custom Field

  • Posts: 197
  • Thank you received: 13
2 years 4 months ago #337323

-- HikaShop version -- : 4.4.4
-- Joomla version -- : 3.10.3
-- PHP version -- : 7.4
-- Browser(s) name and version -- : Chome

Hi.
For some reason, I want to manually display my categories Ajax image extrafield.

My extrafield name is "img2"
and I used an image named "tree.jpg" for example

when I put the code
$this->element->img2;

I the file name correctly but I need the full path of image to as the src of my img tag.

how can I get the extrafield with correct tag and not plain file name ??

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

  • Posts: 81361
  • Thank you received: 13037
  • MODERATOR
2 years 4 months ago #337329

Hi,

Supposing we're talking about a custom field of the table "category", you need code like that:

<?php
	$fieldClass = hikashop_get('class.field');
	$field = $fieldClass->getField('img2', 'category');
	echo $fieldClass->show($field,$this->element->img2);
?>

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

  • Posts: 197
  • Thank you received: 13
2 years 4 months ago #337331

Thank you.

I put this code in "listing_img_title.php" file and got the following error :

0 - Call to undefined method hikashop::show()

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

  • Posts: 81361
  • Thank you received: 13037
  • MODERATOR
2 years 4 months ago #337332

Hi,

I don't see how the code I gave can generate that error message.
Either you made a typo, or the error comes from somewhere else.
Please double check.
And if you don't see the problem, please activate the debug system setting of the Joomla configuration and try again. You'll get an error with more information on what's going on. In that case, please also provide a screenshot of the modification you've made.

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

  • Posts: 197
  • Thank you received: 13
2 years 4 months ago #337356

I checked the code multi times.

the error refers to the third line of the code :
echo $fieldClass->show($field,$this->element->img2);

when I comment this code, error message disappears

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

  • Posts: 197
  • Thank you received: 13
2 years 4 months ago #337357

I turned on debug system and atached the screenshot :

Attachments:

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

  • Posts: 197
  • Thank you received: 13
2 years 4 months ago #337358

That was a kiddy mistake by me ... the code worked perfect. thank you.

my last question is can i use this code for product page as well by replacing the field name and changing 'category' to 'product' ?

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

  • Posts: 81361
  • Thank you received: 13037
  • MODERATOR
2 years 4 months ago #337362

Hi,

Well, it depends what you mean.
If you want to display the value of a custom field of the table "category" for the category of the current product, it's possible but you need to leave "category" in that code. Instead you want to change

$this->element
by
$this->categories[0]
If you want to display the value of a custom field of the table "product" for the current product, it's possible and you indeed need to change "category" to "product". However, if your product has variants, $this->element will point to the default variant of the product and $this->element->main will point to the main product. So you might need to change that, or make it dynamically use main or not based on weither it's there or not.

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

  • Posts: 197
  • Thank you received: 13
2 years 4 months ago #337364

Thanks alot.

Everything is solved

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

  • Posts: 197
  • Thank you received: 13
1 year 9 months ago #342411

Hi Nicolas.

Previously you sent this great piece of code to me which was very useful to display custom fields.

Now i have a problem.
when using product variants, when I change the characteristic, the value of displayed custom field doesnt change the way it changes when using default custom fields layout.

is there any solution for that problem as well ?

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

  • Posts: 81361
  • Thank you received: 13037
  • MODERATOR
1 year 9 months ago #342416

Hi,

There is, but it's more complex.
You need to wrap your custom field in a div with an id starting with hikashop_product_ and ending wih _main

You also need to loop on the $this->element->variants array to call $fieldClass->show on each element and wrap each HTML around a div with a display:none on it, an id which is similar to the main div but with the id of each value for the current variant instead of "main". I would recommend to do this in product / show as there is already a loop for all the rest of the variant data there with the variable $variant_name you can use instead of calculating the ids of the variants yourself.

And lastly, you need to modify the javascript in product / show_block_characteristic to add to this array

var names = ['id','name','code','image','price','quantity','description','weight','url','width','length','height','contact','custom_info','files'];
the text you have between hikashop_product_ and _main in the id of the main div.

So it's totally possible but instead of 1 line of code at one place, you need several lines in several places.

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

Time to create page: 0.071 seconds
Powered by Kunena Forum