liquid - affect collection filters count in shopify
Solution:
I think what you need to do is the following:
- Loop through all products in your collection
- Filter through them using your tag
- Assign an index to each of those products
- Then print the last recorded index
- List item
In liquid this look pretty much like this:
{% for filter in collection.filters %}
{% if product.tags contains 'test' %}
{% if forloop.last %}
{{forloop.index0}}
{% endif%}
{%endif %}
{% endfor%}
Answer
Solution:
In the for-loop that you use to render products you can create acount
variable and use it later.
pseudocode:
{% liquid
assign counter = 0
for product in products
assign counter = counter | plus: 1
render 'product-div'
endfor
render 'counter', count: counter
%}
this solution isn't elegant but the easiest one. If you need a counter in 'product-div' you can create two exact same for-loops: one for counting and second one for rendering.
Again, this solution is not the most clever one but the beauty lies in simplicity and simple code is always the best one
Source
Didn't find the answer?
Our community is visited by hundreds of Shopify development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.