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.
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 %}
...
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.
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 }}
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.
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.
User | Count |
---|---|
25 | |
22 | |
22 | |
19 | |
12 |