- Posts: 240
- Thank you received: 13
Display Custom Field
4 years 10 months ago #337323
by khashiz
Display Custom Field was created by khashiz
-- 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 ??
-- 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.
4 years 10 months ago #337329
by nicolas
Replied by nicolas on topic Display Custom Field
Hi,
Supposing we're talking about a custom field of the table "category", you need code like that:
Supposing we're talking about a custom field of the table "category", you need code like that:
Code:
<?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.
4 years 10 months ago #337331
by khashiz
Replied by khashiz on topic Display Custom Field
Thank you.
I put this code in "listing_img_title.php" file and got the following error :
0 - Call to undefined method hikashop::show()
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.
4 years 10 months ago #337332
by nicolas
Replied by nicolas on topic Display Custom Field
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.
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.
4 years 10 months ago #337356
by khashiz
Replied by khashiz on topic Display Custom Field
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
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.
4 years 10 months ago #337357
by khashiz
Replied by khashiz on topic Display Custom Field
I turned on debug system and atached the screenshot :
Please Log in or Create an account to join the conversation.
4 years 10 months ago #337358
by khashiz
Replied by khashiz on topic Display Custom Field
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' ?
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.
4 years 10 months ago #337362
by nicolas
Replied by nicolas on topic Display Custom Field
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
by
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.
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
Code:
$this->element
Code:
$this->categories[0]
Please Log in or Create an account to join the conversation.
4 years 10 months ago #337364
by khashiz
Replied by khashiz on topic Display Custom Field
Thanks alot.
Everything is solved
Everything is solved
Please Log in or Create an account to join the conversation.
4 years 3 months ago #342411
by khashiz
Replied by khashiz on topic Display Custom Field
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 ?
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.
4 years 3 months ago #342416
by nicolas
Replied by nicolas on topic Display Custom Field
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
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.
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
Code:
var names = ['id','name','code','image','price','quantity','description','weight','url','width','length','height','contact','custom_info','files'];
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.219 seconds