Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
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 }}
Yeah collection1 returns a number and collection2 returns nothing. {{collection2}} also returns EmptyDrop if I try to output it.
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.
Unfortunately still not working for me, very nice try though!
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.
As 2024 wraps up, the dropshipping landscape is already shifting towards 2025's trends....
By JasonH Nov 27, 2024Hey Community! It’s time to share some appreciation and celebrate what we have accomplis...
By JasonH Nov 14, 2024In today’s interview, we sat down with @BSS-Commerce to discuss practical strategies f...
By JasonH Nov 13, 2024