News:

You may pay someone to create your store, or you visit our seminar and become a professional yourself with the silver certification

Main Menu

Using Boolean Custom Fields in Templates

Started by LoneWolf2k1, July 16, 2014, 11:42:33 AM

Previous topic - Next topic

LoneWolf2k1

Hi,

not sure if it's my lack of understanding PHP or VM - maybe someone could point me in the right direction?

I'm trying to use a boolean custom field to create a condition wether a part of HTML-Code is shown or not.

My (shortened) Code-Attempt so far:
if (($this->product->customfieldsSorted['Tipp'])==true){echo'Yup';}else{echo'Nope';}

The point is to show "Yup" if the product's custom field "Tipp" is set to true, "Nope" if it isn't. Above code only shows "Nope", no matter what the boolean value is set to.

Anyone got an idea?

Thanks a lot!

edit: Maybe this thread shold be in "Templates" instead- I created it here due to the "Custom Field Types (Explained)"-Thread here. Mods, please feel free to move if you think it's better located there. Sorry!

AH

 Try this


if (!empty($this->product->customfieldsSorted['Tipp'])){
        echo'Yup';
} else {
        echo'Nope';
       }
Regards
A

Joomla 4.4.5
php 8.1

LoneWolf2k1

I tried that earlier - sadly that doesn't work either, output is always "Nope"... :/

AH

Have you debugged/checked the product array?

Just to see what is in your array value of Tipp
echo "This is my customfield" . $this->product->customfieldsSorted['Tipp']));

Actually my first code was rubbish -  use this. ['Tipp']


if ($this->product->customfieldsSorted['Tipp']){
        echo'Yup';
} else {
        echo'Nope';
       }
Regards
A

Joomla 4.4.5
php 8.1

LoneWolf2k1

#4
Okay, I finally got it to work- thanks for suggesting the debugging. Sometimes it's the obvious choices you think of last.

Seems that, unless a custom field is assigned a layout position, it's not accessible, no matter if it's supposed to output anything or not.

The solution (after assigning a position) seems a bit too complicated still... not sure if it's supposed to work that way?

if (($this->product->customfieldsSorted['Tipp'][0]->custom_value)==1){
        echo'Yup';
} else {
        echo'Nope';
       }

It seems that, even though the field is declared as boolean, the critical value is the field-value of 'custom_value' in sub-array 0...

Thanks a lot for the help!