2
votes

As I'm looping through a collection in Shopify, I want to add that product to another array of products so that I can loop through it again.

{% assign custom_products = '' %}
{% for product in collections['all'].products %}
    {% assign custom_products = custom_products | append: product %}
{% endfor %}

But when I iterate through it again I get nothing

{% for product in custom_products %}
{% endfor %}

When I dump custom_products I get ProductDropProductDropProductDropProductDrop... and so on. Is this because I am constructing as a string? I want the second for loop in the Liquid template to move through the products as if it were collections['all'].products. Any ideas?

1
Your example is not clear. You are looping through the all products collection, trying to duplicate it. Duplicating collections is best done using Shopify, not Liquid.David Lazar
@DavidLazar I am conditionally appending products to the custom array. My question is how to maintain the format so that I can continue looping the my custom array down the line.JLF

1 Answers

1
votes
{% capture custom_products %}
  {% for product in collections['all'].products %}
    {{ custom_products }},{{ product.handle }}
  {% endfor %}
{% endcapture %}

{% assign custom_products = custom_products | split: ',' %}

{% for product in custom_products %}
  {{ all_products[product].title }}
{% endfor %}