1

I am trying to make an array which already has some tags.. I want to loop through each product in cart and add the tags to the array. The {{tag }} part is working but its not getting assigned to the array..

{% assign finalTaglist = "apples, oranges, peaches" | split: ", " %}
        
        {% for item in cart.items %}
          <p>
            {% for tag in item.product.tags %}
                {{tag}}
            
                {% assign finalTaglist = finalTaglist | concat: tag %}
            
            {% endfor%}
          </p>
        {% endfor%}
        
        <p>Final Tag List : {{finalTaglist}}</p>`

`

3 Answers 3

7

concat is used for joining arrays, but your code is trying to add a string to an array, hence why it doesn't work.

Start with an empty string, and use append to add on to the string, not forgetting your separator. Once built, use split to create the array, then you can append to another array if required.

Something along these lines (not tested, just winging it, but you get the idea..)

{% assign finalTaglist = 'apples,oranges,peaches' | split: ',' %}

{% assign newTagList = '' %}
{% for item in cart.items %}
    {% for tag in item.product.tags %}
        {% assign newTagList = newTagList | append: ',' | append: tag %}
    {% endfor%}
{% endfor%}

{% assign newTagList = newTagList | remove_first: ',' | split: ',' %}

{% assign joinedTagLists = finalTaglist | concat: newTagList %}
2
  • thankyou stackoverflow.com/users/10706609/steview Commented Aug 13, 2021 at 21:23
  • btw, this seems to work only with literal strings but not with an object such as ArticleDrop objects. It will concat the literal string ArticleDrop and not the object with its keys.
    – Fed
    Commented Jan 24 at 17:20
1

For anyone landing here in recent times, a better solution IMHO is to use the sort filter on a non-array to turn it into an array with just that item, then you can pass it to concat.

{% assign finalTaglist = "apples,oranges,peaches" | split: "," %}
{% for item in cart.items %}
    <p>
        {% for tag in item.product.tags %}
            {{tag}}
        
            {% assign tag_in_array = tag | sort %}
            {% assign finalTaglist = finalTaglist | concat: tag_in_array %}
        {% endfor%}
    </p>
{% endfor%}
    
<p>Final Tag List : {{finalTaglist | json}}</p>`

Source: https://ellodave.dev/blog/article/advanced-liquid-arrays/ (David Warrington)

2
  • I tried your way but facing with error: json not allowed for this object
    – huykon225
    Commented Sep 20, 2023 at 3:01
  • Your answer didn't quite work for me because it led to unwanted sorting, but it did inspire me to try simply concatenating the array with a string which works (see my answer) Commented Sep 28, 2023 at 17:41
0

This worked best for me. I created an empty array and then just used concat on the strings I wanted to add to the list. It seemed to work just fine with shopify, liquidjs, and the .NET nuget packages I'm using.

{% assign statuses = "" | split: "" %}
{% for entity in myCollection %}
    {% if entity.houseIsOpen %}
        {% assign statuses = statuses | concat: "Open" %}
    {% endif %}
    {% if entity.houseIsClosed %}
        {% assign statuses = statuses | concat: "Closed" %}
    {% endif %}
    {% if entity.houseIsExpired %}
        {% assign statuses = statuses | concat: "Expired" %}
    {% endif %}
{% endfor %}
{{ statuses | uniq | join: ", " | default: "INVALID" }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.