skip to Main Content

A company that I’m doing work with has added custom tags to each of my products. The problem I’m having is trying to find a meaningful way to work with this data.

product.tags give me this.

value1:abc
value2:def
value3:ghi
value4:jkl

My problem is that each of these tags is a complete string.

I need to figure out a way to say — if value1 == "something" do "this" end —

I’ve tried seeing if I can make something happen with product.tags | JSON, but as you can imagine that doesn’t really help either. I’ve looked up working with the products API and how to achieve what I want– but it seems like there might be an easier way to achieve this.

Can anyone provide any insight?

The end result is I need to display specific copy on my product page depending on what those tags values (after the : ) are.

2

Answers


  1. So:

    {% for tag in product.tags %}
        {% assign tag_array = tag | split:':' %}
        {% if tag_array.last == 'myvalue' %}
            Do something
        {% else %}
            {% continue %}
        {% endif %}
    {% endfor %}
    

    Explanation: first you loop through tags to get tag string value.
    Then you split tag to get an array, then you retrieve the value and use it in you conditional statement.

    Login or Signup to reply.
  2. Similar answer to @Alice Girard, I would use case and when instead because of the amount of tags you need to define.

    {% for tag in product.tags %}
        {% assign tag_array = tag | split: ":" %}
        {% case tag_array.first %}
        {% when 'value1' %}
            do something
        {% when 'value2' %}
            do something
        {% else %}
            default error message
        {% endcase %}
    
        {% case tag_array.last %}
        {% when 'abc' %}
            do something
        {% when 'def' %}
            do something
        {% else %}
            default error message
        {% endcase %}
    {% endfor %} 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search