Less than condition in if loop not working - Liquid HTML - shopify

In liquid html greater than condition works well in if loop
{% assign var1 = product.extended_attributes.inventory | plus: 0 %}
{% if var1 > 5 %}
test text
{% endif %}
This condition works. But i want to check for less than condition
{% assign var1 = product.extended_attributes.inventory | plus: 0 %}
{% if var1 < 5 %}
test text
{% endif %}
This code breaks the liquid html syntax in < so this condition not working.
Note: this happens on blueshift email template. Not sure about others.

It probably is an app specific bug. Try this instead
{% assign var1 = product.extended_attributes.inventory | plus: 0 %}
{% unless var1 >= 5 %}
test text
{% endunless %}

Related

On Shopify, check if a given product is not on any collection

Within the product page, I would like to hide a certain element if the current product isn't associated with any collection. Is there any way to check it with Liquid ?
The object product.collections is an array containing all collections where the product is listed.
So you may use this:
{% if product.collections.size < 1 %}
Do something
{% endif %}
Documentation about arrays here:
https://shopify.dev/api/liquid/filters#size
{% if 0 == product.collections | size %}
!! put your code here !!
{% endif %}
Or
{% for _ in product.collections %}
{% else %}
!! put your code here !!
{% endif %}

Django template syntax error: could not parse remainder % 2

I am getting a TemplateSyntaxError: "could not parse remainder % 2 from num%2":
{% if num%2 ==0 %}
{{"Even"}}
{% else %}
{{"Odd"}}
{% endif %}
You can't use arbitrary Python expressions in Django templates. You should create a custom filter for them.
However, for your expression there is a built-in tag divisibleby. From its example:
{{ value|divisibleby:"2" }}
If value is 4, the output would be True. So the final answer looks like (untested):
{% if num|divisibleby:"2" %}
Even
{% else %}
Odd
{% endif %}

Liquid: How to assign the output of an operator to a variable?

I'm working with Liquid templates for Shopify. I want some elements to show up only if the month happens to be December. Since there are multiple elements that need this, I want to set a variable at the top of the document and refer to it later. Here's what I've got that's working:
<!-- At the top of the page -->
{% assign month = 'now' | date: "%m" %}
{% if month == "12" %}
{% assign isDecember = true %}
{% else %}
{% assign isDecember = false %}
{% endif %}
<!-- Only show in December -->
{% if isDecember %}
Happy Holidays
{% endif %}
This works (for testing I change "12" to the current month) but it is pretty ugly. In most languages I would do something like this:
{% assign isDecember = (month == "12") %}
Liquid doesn't accept parentheses, so obviously this won't work. And it doesn't work without parentheses either. The documentation has examples for using operators and for assigning static values to variables, but nothing about combining the two.
I can assign the output of a | filter to a variable, but there don't seem to be filters to cover every operator (or even the necessary "=="), so that's unsatisfactory.
Is there any way to assign the output of an operator to a variable in Liquid?
There isn't a way to do that elegantly and according to this, they won't support ternary operators. There's a mention of someone trying a similar thing.
A slightly shorter/different version would be:
{% assign month = 'now' | date: "%m" %}
{% liquid
case month
when '12'
assign isDecember = true
else
assign isDecember = false
endcase %}
You could altogether avoid using an intermediary boolean flag variable isDecember as Liquid assign with only boolean variables seems to not working within if/endif. Here are the solutions.
Just use plain strings:
{% assign month = 'now' | date: "%m" %}
{% if month == "12" %}
Happy Holidays
{% endif %}
Or use plain string assignments (not boolean values assignments) within ifs:
{% if month == "12" %}
{% assign phrase = "Happy Holidays" %}
{% else %}
{% assign phrase = "Happy usual time of the year" %}
{% endif %}
Now my message to you is: {{ phrase }}
Still want unsing intermediary isDecember? If you would put some dummy text assignment within either clause of if/else that would work too.
{% if month == "12" %}
{% assign dummy = "summy" %}
{% assign isDecember = true %}
{% else %}
{% assign isDecember = false %}
{% endif %}
Hope that helps.

Shopify - Increment or Counter

I'm trying to set up a simple way to increment within for loops in my themes and can't figure out how to get it to work. I'm familiar with two ways to increment:
{% assign variable = 0 %}
{% for .....
{% assign variable = variable | plus: 1 %}
.... endfor %}
and
{% assign variable = 0 %}
{% increment variable %}
However neither of these work. Update: Currently the following block of code will output "0" when it should be "1"
{% assign variable = 0 %}
{% assign variable = variable | plus: 1 %}
{{ variable }}
What am I doing wrong?
What you are doing with the assign should work however there is an easier way:
{{forloop.index0}}
See the docs for the loop object

Looping through metafields in shopify

I need to display n number of images for a product in shopify.
I have stored number of images in a metafields and created a loop for it.
Then each image's name is stored in a metafield, which i am trying to get with help of loop.
{% assign earrings = product.metafields.earrings %}
{% for i in (1..earrings.total-earrings) %}
{% assign earring = 'product.metafields.earring-' | append:i %}
{{ earring.name }}
{% endfor %}
This loop is giving me values for earring like:
product.metafields.earring-1
product.metafields.earring-2
but when i am trying to read value of metafield earring.name, i am not getting any output. I think because product.metafields.earring-1 is a string.
Is there any possible way to loop through metafields like this and get values?
Just in case it's helpful for someone.
Here's the updated code:
{% assign earrings = product.metafields.earrings %}
{% for i in (1..earrings.total-earrings) %}
{% assign dummy = 'earring-' | append:i %}
{% assign earring = product.metafields[dummy] %}
{{ earring.name }}
{% endfor %}