Solved

Is it possible to add two variable to check in an if statement snippet?


Badge +3

Howdy!

Is it possible to set custom varibles tough the templating language like this?

{% if <condition> %}
{% variable_name = <some value> %}
{% else %}
{% variable_name = <some other value> %}
{% endif %}

That would be extremely useful to not have to duplicate large chunks of templating code.

If that is not directly supported, is there any hack for achieving a similar result?

icon

Best answer by Taylor Tarpley 11 May 2022, 22:10

View original

2 replies

Userlevel 7
Badge +60

Howdy @Henrik Sommerland

 

Thanks for sharing your question with us! I would love to provide clarity on this. 

 

While I can definitely understand why you’d want to find a workaround for this, unfortunately while you can use one variable within your if statement, you cannot add an additional variable within the snippet. Django doesn’t support his function and therefore will only successfully fire with one variable at a time.

 

Do you mind sharing what your goal is or what inconvenience you’re trying to workaround? This might help me better discover if there’s another way to accomplish what you want. For example, if you are creating if statements to check in an individual item’s data, you can instead create an items.collection tag, that would search through a grouping of items in one action, instead of creating multiple duplicate chunks of code snippets as you mentioned before. 

 

 Thanks for participating in the Community! 

-Taylor

Badge

@Henrik Sommerland Did you ever find a workaround for this? 

 

@Taylor Tarpley Here is my use case:

 

I am trying to find the highest priced item in the line item list from a started checkout event. The only way that I can think of to do this would be to assign a variable. Here is what the code looks like. Granted, it doesn’t work, but I’m curious if there is a workaround:

 

{% with mm_product_name= event.extra.line_items.0.presentment_title %}
{% with mm_product_price= event.extra.line_items.0.price %}
{% for item in event.extra.line_items %}
{% if item.price > mm_product_price %}
{% with mm_product_name= item.presentment_title %}
{% with mm_product_price= item.price %}
{% endif %}
{% endfor %}

 

 

Essentially, I would then use the mm_product_name and mm_product_price variables later in my email template.

 

Shopify very similarly to this with liquid code: https://shopify.github.io/liquid/tags/variable/

 

 

 

Reply