If collection with name exists

Tourist
18 0 2

I'm trying to turn my product vendor name into a link to a collection with the same name, but only if the collection exists.

    {% if settings.product_vendor_enable  %}
      <p class="product-meta" itemprop="brand">
      {% if collections[product.vendor].products_count != "0" %}
        <a href="{{shop.url}}/collections/{{product.vendor | handleize }}"> {{ product.vendor }}</a>
      {% else %}
        {{ product.vendor }}
      {% endif %}
      </p>
    {% endif %}

But it seems that calling `collections["some name"]` will always return a valid collection object. made worse by the fact that for some godawful reason, the `collection.all_products_count` and `products_count` are strings, not numbers, so it throws "cannot compare String and 0" when I try to ask if there are any prouducts in the collection. What's the correct way to do this?

0 Likes
Astronaut
1890 0 373

Try this:

      {% if collections[product.vendor] != blank %}

 

0 Likes
Tourist
18 0 2

nope. That didn't do it. always returns false, even if the collection exists.

0 Likes
Astronaut
1890 0 373

I didn't check your shop before, but it looks like it's working properly.  Are you still having problems?

0 Likes
Tourist
18 0 2

Here's a product with a matching collection: https://everylittlewar.com/products/beacons

Here's a product without a matching collection: https://everylittlewar.com/products/superglue

They are both showing the vendor name as not-a-link:

    <h1 class="h2" itemprop="name">{{ product.title }}</h1>
    {% if settings.product_vendor_enable  %}
      <p class="product-meta" itemprop="brand">
      {% if collections[product.vendor] != blank %}
        <a href="{{shop.url}}/collections/{{product.vendor | handleize }}"> {{ product.vendor }}</a>
      {% else %}
        {{ product.vendor }}
      {% endif %}
      </p>
    {% endif %}

 

0 Likes
Astronaut
1890 0 373

You will want to handleize product.vendor before it goes into collections[...].  Try this:

    <h1 class="h2" itemprop="name">{{ product.title }}</h1>
    {% if settings.product_vendor_enable %}
      {% assign vendor_handle = product.vendor | handleize %}
      <p class="product-meta" itemprop="brand">
      {% if collections[vendor_handle] != blank %}
        {{ product.vendor | link_to: collections[vendor_handle].url }}
      {% else %}
        {{ product.vendor }}
      {% endif %}
      </p>
    {% endif %}

 

1 Like
Tourist
18 0 2

Perfect! now.... why did that work?

0 Likes
Highlighted
Astronaut
1890 0 373

You need to insert the collection's handle into collections[...], and not the collection's title.

Before, it was effectively doing this:

collections["Underground Lasers"]

This will output a null collection because there is no collection with the handle "Underground Lasers".  It is impossible for any handle to have spaces or capital letters.

The updated code outputs a valid collection.

collections["underground-lasers"]

 

0 Likes
Tourist
18 0 2

thanks! I wish the "collections" and other object-group-accessor-objects were documented.....

0 Likes