Page 1 of 1

[SOLVED] Products, smarty if value

Posted: Thu Apr 10, 2014 10:21 am
by thomahawk
I have a little smarty trouble here, as a non programmer.

I have an extra field for the products, which when activated means the product is not available anymore (vergriffen) and therefore the "add to cart" button is not displayed on that product. The code in the products list template is s follows:

{if $entry->fields.vergriffen == true}
vergriffen
{else}
{Cart2 sku=$entry->sku} <div class="preis">CHF {$entry->price|number_format:2}</div>
{/if}


In my understanding this will show the text "vergriffen" if the field value is = true. And in all other cases it shows the button

And it works so far. BUT, only if there is no other value present. There have been cases where once the field has been activated and deactivated and the database ends up containing "false" for this product. Strange that in that case it also displays "vergriffen", but it must show the "add to cart" button.

Only if I delete the entry in the database, it works properly again. Not even leaving it blank works.

So what did I wrong in the smarty code? I mean, if its not "true" but in any other case (blank, false, dummydumb or whatever) the "else" statement should take effect. Or not?

Thanks for any enlightment on the matter.
Thom

Re: Products, smarty if value

Posted: Thu Apr 10, 2014 10:27 am
by Jo Morg
It seems that the field is a text field so "false" will validate as true...

Maybe

Code: Select all

{if isset($entry->fields.vergriffen) && $entry->fields.vergriffen == 'true'}

Re: Products, smarty if value

Posted: Fri Apr 11, 2014 6:40 am
by thomahawk
Hi Jo. Thanks for the input.
But unfortunately it does not work. Your code makes all products available.

Backgroud information: Most products have no entry for this field in the database, because the items got registered in "Products" but the "vergriffen" field never used. Two have the entry "true" (because they are not available anymore), one has the entry "false" (I did set it to true once and then deactivated the "vergriffen" field again, this results in "false" stored in the database).

I see what you mean, I have to address the value of that field. I thought my original code did that, but obviously thats not the case. I also tried {if $entry->fields.vergriffen == 'true'} but this produces page error.

Re: Products, smarty if value

Posted: Fri Apr 11, 2014 8:16 am
by velden
I also tried {if $entry->fields.vergriffen == 'true'} but this produces page error.
The error message could be helpful. Make sure to test the page in the same browser you logged in to cms.

<pre>{$entry->fields.vergriffen|print_r}</pre> should print out useful information about the field. Then you know what you should check for (e.g. boolean or string)

try (assumes a boolean):

Code: Select all

{if isset($entry->fields.vergriffen) && $entry->fields.vergriffen->value}
or (assumes a string)

Code: Select all

{if isset($entry->fields.vergriffen) && $entry->fields.vergriffen->value == 'true'}

Re: Products, smarty if value

Posted: Fri Apr 11, 2014 8:43 am
by thomahawk
Great!

{if isset($entry->fields.vergriffen) && $entry->fields.vergriffen->value == 'true'}

This has solved the problem perfectly!
Thank you very much, velden (once again ;-)

Thom