skip to Main Content

I’m working on custom solution for Shopify store and my problem is that I can’t manage how to avoid adding ‘ / ‘ after the last matched tag in the case statement.

I’ve tried to use some if statements with a forloop filters with no result.
The last thing on my mind was finding the last matched tag in the loop that will help us avoiding ‘ / ‘ after the last element, but unfortunately I can’t manage how to do that.

Expected result: Wool/Nylon/Viscose

Here is the part of the code that compares all tags assigned to the product with the list of tags(clothing materials) required for the output.
Considering that product has such tags as Wool, Nylon and Viscose and others, non material.

Example 1

Actual & Expected Result: WoolNylonViscose

{% for tag in product.tags %}
{% case tag %}
    {% when 'Viscose' %}
    Viscose
    {% when 'Wool' %}
    Wool
    {% when 'Polyamide' %}
    Polyamide
    {% when 'Nylon' %}
    Nylon
    {% else %}
{% endcase %}
{% endfor %}

Example 2

Filter forloop.last was used to define the last element of the loop, but the problem is that material tags(Wool, Nylon, Viscose) can be in the middle of the product tag array. Considering product has 10 tags and material tags are spread among the array we will see next result.

Result: ///Wool/Nylon////Viscose//

{% for tag in product.tags %}
    {% if forloop.last == true %}
        {% case tag %}
                {% when 'Viscose' %}
                Viscose
                {% when 'Wool' %}
                Wool
                {% when 'Nylon' %}
                Nylon
                {% else %}
        {% endcase %}
    {% else %}
        {% case tag %}
                {% when 'Viscose' %}
                Viscose
                {% when 'Wool' %}
                Wool
                {% when 'Nylon' %}
                Nylon
                {% else %}
        {% endcase %}
        /
    {% endif %}
{% endfor %}

I would appreciate if you could please point on my mistakes and suggest me how can i achieve solution.

2

Answers


  1. You can try the following:-

    {% for tag in product.tags %}
    {% case tag %}
        {% when 'Viscose' %}
         {{ 'Viscose' | append: '/' }}
        {% when 'Wool' %}
         {{ 'Wool' | append: '/' }}
        {% when 'Polyamide' %}
         {{ 'Polyamide' | append: '/' }}
        {% when 'Nylon' %}
         {{ 'Nylon' }}
        {% else %}
    {% endcase %}
    {% endfor %}
    
    Login or Signup to reply.
  2. This should work:

    {% capture tag_string %}{% endcapture %}
    {% for tag in product.tags %}
      {% if tag == 'Viscose' %}{% capture tag_string %}{{ tag_string }}Viscose/{% endcapture %}
      {% elsif tag == 'Wool' %}{% capture tag_string %}{{ tag_string }}Wool/{% endcapture %}
      {% elsif tag == 'Polyamide' %}{% capture tag_string %}{{ tag_string }}Polyamide/{% endcapture %}
      {% elsif tag == 'Nylon' %}{% capture tag_string %}{{ tag_string }}Nylon/{% endcapture %}
      {% endif %}
    {% endfor %}
    {{ tag_string | split: "" | reverse | join: "" | remove_first: "/" | split: "" | reverse }}
    

    More on string filters: https://help.shopify.com/themes/liquid/filters/string-filters

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