VirtueMart Forum

VirtueMart 2 + 3 + 4 => Product creation => Topic started by: LoneWolf2k1 on July 16, 2014, 11:42:33 AM

Title: Using Boolean Custom Fields in Templates
Post by: LoneWolf2k1 on July 16, 2014, 11:42:33 AM
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!
Title: Re: Using Boolean Custom Fields in Templates
Post by: AH on July 16, 2014, 12:09:19 PM
 Try this


if (!empty($this->product->customfieldsSorted['Tipp'])){
        echo'Yup';
} else {
        echo'Nope';
       }
Title: Re: Using Boolean Custom Fields in Templates
Post by: LoneWolf2k1 on July 16, 2014, 12:15:14 PM
I tried that earlier - sadly that doesn't work either, output is always "Nope"... :/
Title: Re: Using Boolean Custom Fields in Templates
Post by: AH on July 16, 2014, 12:58:25 PM
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';
       }
Title: Re: Using Boolean Custom Fields in Templates
Post by: LoneWolf2k1 on July 16, 2014, 13:47:32 PM
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!