Hello. I’ve adjusted the code in my theme in order to show the product collection in product cards.
How to make the product collection clickable and go to the corresponding category?
Goal: Make the collection label shown on product cards in the Dawn theme clickable to its corresponding collection page.
Process and attempts:
Final working solution (code-centric):
Styling note:
Status: Functionality resolved with a robust code approach; minor styling question remains open.
Hello. I’ve adjusted the code in my theme in order to show the product collection in product cards.
How to make the product collection clickable and go to the corresponding category?
Could you share your page URL to check?
Give me the code you added to display collection title
On card-product.liquid
Without investigating your store code we couldn’t not help you
Update the code to this.
{{ card_product.type | escape }}
It works. But the link points to the page the product is currently (if the product is featured on homepage, it points on homepage). Also, the text is blue, can we make it to #fc7060 like previously?
Please try to update code to this and check again
{% assign primary_collection = card_product.collections.first %}
{{ card_product.type | escape }}
So this code was close but ultimately doesn’t work as intended when the product has multiple categories. Also Shopify’s internal ordering is seemingly random and you can’t influence it, so it can go to best sellers collection etc.
I troubleshoot it with ChatGPT and with your help this code works correctly.
{% assign collection_url = nil %}
{% for collection in card_product.collections %}
{% if collection.title == card_product.type %}
{% assign collection_url = collection.url %}
{% break %}
{% endif %}
{% endfor %}
{% if collection_url %}
{{ card_product.type | escape }}
{% else %}
{{ card_product.type | escape }}
{% endif %}