How can I effectively link to a vendor's collection on my website?

Hello, on my site im trying to link every vendor under my product which would test if there is a collection with the vendor name and display a link to the collection. If there is no collection it will link to a url with “/collections/vendors?q=”.

Currently this is the part that works:

Brand: {{ product.vendor | link_to_vendor }}

Which links to a url with “/collections/vendors?q=”

Here’s my code that i’ve tried but doesn’t work.

{% if product.collections contains product.vendor %}

Brand: {{product.vendor}}

{% else%}

Brand: {{ product.vendor | link_to_vendor }}

{% endif %}
1 Like

Hi K,

you can not use this conditional:

{% if product.collections contains product.vendor %}

since product.collections is not an array of strings, but array of objects.

You have to either loop over product.collections, like this:

{% assign found = false %}
{% for coll in product.collections %}
   {% if coll.title == product.vendor %}
      {% assign found = true %}
      {% break %}
  {% endif %}
{% endfor %}

{% if found %}
  ...

Or use a map function:

{% assign coll_titles = product.collections| map: 'title' %}
{% if coll_titles contains product.vendor %}
  ....
1 Like

Thanks so much Tim your code works great!

Hey Kyritheous - I posted an alternate solution in a blog post here: https://aloagency.com/blog/shopify-vendor-link-collection

3 Likes

Hi.
Would it be possible to split the vendor string into arrays?
I’m using the product vendor thing to indicate author(s) of the books I’m selling rather than the publisher.
Would it be possible to add some variation of | split: ‘and’
into this code:

{% if section.settings.product_vendor_enable %}

{{product.vendor | link_to_vendor}}

{% endif %}

so authors would show as separate links?

1 Like

Hey,

When I click vendor that show: myshopify.com/collections/vendors?q=vendor-name

But I want to like: myshopify.com/vendor/vendor-name or myshopify.com/collection/vendor-name

Is it possible?

I’m using Dawn theme.

Sorry figured it out!

This is something I’ve also wanted to do.

I am slightly familiar with editing theme codes, which file would this be added to and what would it replace (if anything)?

Did you ever find a fix to this?

Hi, you’ve posted the part of the code, which helped me a lot.
Thanks.