Shopify - Increment or Counter - shopify

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

Related

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.

specify variable array index for liquid

My liquid code is
{% assign _product_id = product.id %}
{% assign _product_tag = product.collections[0].title}
{% assign _product_name = product.title %}
{{_product_tag}}
{% assign pagla_array = collections[_product_tag].products %}
{{ pagla_array.first.title }}
Here last line showing nothing. if I use a static index for assigning pagla_array like {% assign pagla_array = collections['Beans'].products %} then it show value. What wrong did I make here?
This line:
{% assign _product_tag = product.collections[0].title}
Is not closed correctly. It should end with %}
In addition you should use handles for the collections, not title.
So it should become:
{% assign _product_tag = product.collections[0].handle %}
....
{% assign pagla_array = collections[_product_tag].products %}

Assign a value to array by index in Liquid

I am inside of a bit complex loops and I need to assign a value to an array by index, so that if the value is already there it will replace it, if not it will create it.
So I need to do something like this:
{% assign arr = '' | split: '' %}
{% assign arr[index] = value %}
which is not working, the array is still empty.
Is there any workaround to do this?
There is no direct workaround.
You can always re-create the array with a default value though that would only give you a single value.
One potential work around would be to re-create the source and fill in any missing defaults then re-split into an array
{% assign arr = someValue | split: '' %} <!-- splitting to single chars ? -->
{% assign withDefaults = '' %}
{% for ...%}
{% unless arr[loop.index0] == true %}
{% withDefaults = withDefaults | append : 'defaultValue,' %}
{% else %}
{% withDefaults = withDefaults | append : arr[loop.index0] | append : ',' %}
{% endfor %}
{% assign arr = withDefaults | split: ',' %} <!-- you'll have an extra blank element but that may not matter -->

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 %}

Less than condition in if loop not working - Liquid HTML

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 %}