How to reference a collection with variable instead of string

joeeverett
Tourist
6 0 0

 

Hi everyone!

Any help with this would be much appreciated.

I have a collection in my store named  Merken/Astro. It's handle is merken-astro. When I output it like below it displays as expected.

 {% for product in collections["merken-astro"].products %}
            <div class="grid-item {{ grid_item_width }} ">
              {% render 'product-grid-item' with product as product %} 
            </div>
{% endfor %}

 

However when I capture the same value as such 

 {% assign chosenCol = "merken/" %}
      {% capture vendorVar %}
      {{product.vendor | prepend : chosenCol | handleize}}
      {% endcapture %}

 {{vendorVar}}

 

the output of {{vendorVar}} is merken-astro.

 

Why then do either of these attempts not return the collection?

 {% for product in collections[vendorVar].products %}
            <div class="grid-item {{ grid_item_width }} ">
              {% render 'product-grid-item' with product as product %} 
            </div>

{% endfor %}

 {% for product in collections.vendorVar.products %}
            <div class="grid-item {{ grid_item_width }} ">
              {% render 'product-grid-item' with product as product %} 
            </div>

{% endfor %}

 

Any help would be much appreciated, thanks! Joe. 

0 Likes
LukaszFormela
Excursionist
37 7 9

Hi @joeeverett 

Looks fine to me... It may be a long shot, but how about this instead:

 

{% assign vendorCollection = collections[vendorVar] %}

{% for product in vendorCollection.products %}
...

 

 

Looking for part-time Shopify Dev job | UK timezone
joeeverett
Tourist
6 0 0

thanks @LukaszFormela unfortunately that does not seem to be working either, i was thinking if the value of vendorVar needs to be 'astro-merken' instead of astro-merken.

Thanks for replying.

0 Likes
LukaszFormela
Excursionist
37 7 9

No, what you did is correct. The value of vendorVar is a string, you don't have to wrap it with quotations/apostrophes any more.

It looks strange though, what's the output of both? Just to check whether it's a problem with handle:

 

{% assign collection1 = collections['merken-astro'] %}
{{ collection1.all_products_count }}

{% assign collection2 = collections[vendorVar] %}
{{ collection2.all_products_count }}

 

Looking for part-time Shopify Dev job | UK timezone
joeeverett
Tourist
6 0 0

Yeah collection1 returns a number and collection2 returns nothing. {{collection2}} also returns EmptyDrop if I try to output it.

0 Likes
LukaszFormela
Excursionist
37 7 9

Okay, here's how to fix it. Replace the following code:

{% capture vendorVar %}
{{product.vendor | prepend : chosenCol | handleize}}
{% endcapture %}

with this one:

{%- capture vendorVar -%}{{ product.vendor | prepend : chosenCol | handleize }}{%- endcapture -%}

 

Notice {% %} tags being replaced with {%- -%} to trim any whitespace characters which apparently were added with first solution.

Looking for part-time Shopify Dev job | UK timezone
joeeverett
Tourist
6 0 0

Unfortunately still not working for me, very nice try though!

0 Likes
LukaszFormela
Excursionist
37 7 9

How do you reference the collection now? It should be:

{% for product in collections[vendorVar].products %}
    <div class="grid-item {{ grid_item_width }}">
        {% render 'product-grid-item' with product as product %} 
    </div>
{% endfor %}

I'm pretty sure your handle should work now. Even though it rendered as 'merken-astro' previously, it contained some invisible characters which made it invalid.

Looking for part-time Shopify Dev job | UK timezone
0 Likes