[SOLVED] Products, smarty if value

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
User avatar
thomahawk
Power Poster
Power Poster
Posts: 312
Joined: Fri Jul 25, 2008 10:13 am

[SOLVED] Products, smarty if value

Post 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
Last edited by thomahawk on Fri Apr 11, 2014 8:43 am, edited 1 time in total.
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1978
Joined: Mon Jan 29, 2007 4:47 pm

Re: Products, smarty if value

Post 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'}
"There are 10 types of people in this world, those who understand binary... and those who don't."
* by the way: English is NOT my native language (sorry for any mistakes...).
Code of Condut | CMSMS Docs | Help Support CMSMS
My developer Page on the Forge
GeekMoot 2015 in Ghent, Belgium: I was there!
GeekMoot 2016 in Leicester, UK: I was there!
DevMoot 2023 in Cynwyd, Wales: I was there!
User avatar
thomahawk
Power Poster
Power Poster
Posts: 312
Joined: Fri Jul 25, 2008 10:13 am

Re: Products, smarty if value

Post 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.
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3497
Joined: Mon Nov 28, 2011 9:29 am

Re: Products, smarty if value

Post 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'}
User avatar
thomahawk
Power Poster
Power Poster
Posts: 312
Joined: Fri Jul 25, 2008 10:13 am

Re: Products, smarty if value

Post 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
Post Reply

Return to “Modules/Add-Ons”