Have your say in Community Polls: What was/is your greatest motivation to start your own business?

Re: How to reference a collection with variable instead of string

How to reference a collection with variable instead of string

joeeverett
Shopify Partner
23 4 6

 

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. 

Replies 7 (7)

LukaszFormela
Excursionist
37 7 15

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 %}
...

 

 

Available for part-time theme edits/development | UK timezone
joeeverett
Shopify Partner
23 4 6

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.

LukaszFormela
Excursionist
37 7 15

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 }}

 

Available for part-time theme edits/development | UK timezone
joeeverett
Shopify Partner
23 4 6

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

LukaszFormela
Excursionist
37 7 15

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.

Available for part-time theme edits/development | UK timezone
joeeverett
Shopify Partner
23 4 6

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

LukaszFormela
Excursionist
37 7 15

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.

Available for part-time theme edits/development | UK timezone