We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

Re: How pass a variable into collections[].products?

How pass a variable into collections[].products?

diego10_01
Shopify Partner
10 1 2
Hey!

So I'm making this for loop:

{% for product in collections['nameOfCollection'].products %}
 Do something...
{% endfor %}

Now, it works perfectly, but what if I don't want to hard code the 'nameOfCollection'?
What if instead of that I'd like to put a variable value? How can I do it?

I've tried:
{% for product in collections['{{myVariable}}'].products %}
{% endfor %}

But it doesn't work,

Does anyone have an idea of how to do this?
Reply 1 (1)

Kani
Shopify Partner
468 125 233

Hi @diego10_01 

 

You can do it like this, don't need to add  '{{ }}'

{%- assign collection_handle = 'myVariable' -%}
{% for product in collections[collection_handle].products %}
      {{ product.title }}
{% endfor %}

 

Hey!! ʕ ᵔᴥᵔ ʔ
Please click 'Like' and ' Accept as Solution' to encourage me to continue sharing my expertise. ♡
If you need any technical assistance, feel free to send me a DM. You no longer have to search for answers without getting a response. 🙂