skip to Main Content

I’m having an issue with a for loop in Shopify. I’m sure it used to work, but I can’t get it to work over the number 9 now.

{% assign productTag1 = Availability14 %} (in this example, the product has only 1 tag, which is Availability14)
{% assign avail_stop = false %}
      {% for j in (0..15) %}
        {% assign check_avail = 'Availability' | append:j %}
        {% if productTag1 contains check_avail %}
            {% assign avail_stop = true %}
            {% capture tag_name %}{{check_avail}}{% endcapture %}
            {% break %}
        {% endif %}
      {% endfor %}
      {% if avail_stop %}
        {% assign availability = check_avail | remove:'Availability' | plus:0 %}
      {% endif %}

At the moment, I’m returning 1, not 14. I imagine it’s something to do with the fact 14 includes a 1, but I can’t wrap my head around it.

Any help is appreciated.

2

Answers


  1. You have a {% break %} statement in your if. Once the if becomes true it will exit the loop instantly.

    If you want to skip the next code you must use {% continue %} not {% break %}.

    Login or Signup to reply.
  2. On my mind this is an issue with conditional operator. As you said, 14 contains 1, so why not simply use strict conditional operator like this:

    {% if productTag1 == check_avail %}
        {% assign has_stop = true %}
        {% break %}
    {% endif %}
    

    (or did I miss something?)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search