How to display a specific type of Collection on Product Page as a link

Hello all :slightly_smiling_face:

I am setting up an online record shop and on each product page I would like to display a collection that represents the record label for that product (and I would like it to display as a link to that label collection).

Each product has at least 3 collections assigned (an artist collection, a genre collection, a label collection). Eg for a product by an artist called Aphex Twin:

artist collection name: ‘Aphex Twin’

genre collection name: ‘Techno/IDM’

label collection name: ‘Warp Records’

Unfortunately I have no coding experience. How can i use custom Liquid to display specifically the label collection on the product page? (Is there a way to tag collections somehow so that it can look for ‘label’ type collections?)

I have tried the below but this does not work as the product collection arrays are ordered alphabetically so will just show whichever of the product’s artist/genre/label collections come last alphabetically:

{% assign product_collection = product.collections.last %}
{% if product_collection %}
{{ product_collection.title | link_to: product_collection.url }}
{% endif %}

I also tried prefixing the names of all label collections with the word ‘label’ eg collection name is “Label: Warp Records” and then using some code to search for any collection for that product with the word ‘Label’ in eg:

{% for collection in product.collections %}
    {{collection.title | where: 'Label' }}
{% endfor %}

(Although haven’t figured out how to turn that into a link - and I would rather avoid using a prefix like this if possible).

Just wondering if anybody could provide some guidance/advice/instructions on how best to do this.

Many thanks in advance for your help!

Justin / Spectrum City

PS site is not live yet

This should do the trick:

{% for collection in product.collections %}
  {% if collection.title contains "label" %}
    {{ collection.title }}
  {% endif %}
{% endfor %}

as long as you have “label” in the collection title. This will display the entire collection title though, so if your collection title is “Label: Warp Records” it will display “Label: Warp Records”.

If you only want to display the label name, use this:

{% for collection in product.collections %}
  {% if collection.title contains "label" %}
    {% assign title = collection.title | split: ": " %}
    {{ title[1] }}
  {% endif %}
{% endfor %}

The split will split the string coming from collection.title into an array that looks like this [‘Label’, ‘Warp Records’] and assign it to title. Then when you want to extract ‘Warp Records’ from the array you use {{ title[1] }}. (Don’t worry about this if you don’t know how arrays work)

1 Like

Hi Kajderuyter - many thanks for your quick reply!

The second option works for me :slightly_smiling_face:

Best regards

Justin

and how to specify type like I have my product which is in 3 collection 2 are automated by tags and 1 is manual i want to only display the collection which is manual.

    {% for collection in product.collections %} {% if collection.products.size > 0 %}
  • {{ collection.title }}

  • {% endif %} {% endfor %}

but when i do this it shows me all three collection name I just want to show the collection title whose type is manual not automated

Kindly advise where to paste this code? I have multiple liquid files in dawn theme and I want to show “label” on all pages and on all sections instead of collection name. Thanks in advance.