Shopify themes, liquid, logos, and UX
I would like to loop through a particular collection, returning the first product that matches a given tag (slides) and the first product that doesn't match.
Solved! Go to the solution
This is an accepted solution.
Something like this. Depending on how large your collection is do be aware the loop might not touch all products. It will only look at the current paginated set, or at most 1000 products.
{% assign slidesProduct = false %}
{% assign generalProduct = false %}
{% for product in collection.products %}
{% if slidesProduct and generalProduct %}{% break %}{% endif %}
{% if product.tags contains 'Slides' and slidesProduct == false %}
{% assign slidesProduct = product %}
{% continue %}
{% elsif generalProduct == false %}
{% assign generalProduct = product %}
{% continue %}
{% endif %}
{% endfor %}
The above is not tested and smashed out on mobile so take it as inspiration only. You'll end up with two variables that are either false, or have product info inside. You should be able to take that and do something with that.
This is an accepted solution.
Something like this. Depending on how large your collection is do be aware the loop might not touch all products. It will only look at the current paginated set, or at most 1000 products.
{% assign slidesProduct = false %}
{% assign generalProduct = false %}
{% for product in collection.products %}
{% if slidesProduct and generalProduct %}{% break %}{% endif %}
{% if product.tags contains 'Slides' and slidesProduct == false %}
{% assign slidesProduct = product %}
{% continue %}
{% elsif generalProduct == false %}
{% assign generalProduct = product %}
{% continue %}
{% endif %}
{% endfor %}
The above is not tested and smashed out on mobile so take it as inspiration only. You'll end up with two variables that are either false, or have product info inside. You should be able to take that and do something with that.
This is plenty to get me started. Thank you, Jason!
Portrait of Stephen positioned next to an image of planet Earth, with the Stephen's World ...
By JasonH Mar 18, 2024Digital marketers and app developers have tracked activity in apps and websites for yea...
By Ollie Mar 13, 2024February was an exciting month with Shopify Editions, informative webinars, and more! F...
By JasonH Mar 7, 2024