Parsing product name for targeted return link

Solved
vereinsshop
Tourist
7 0 0

Hello everyone!

Awesome community you guys have going on here!
I am hoping someone can help me with the following problem: I have inserted a "back to collection" button on my product page. This button basically checks the collection and the href sends the user back to that collection. However, if a product is accessed through the "product suggestions" at the bottom of the page, the button disappears. The collection also doesnt appear in the URL path (obviously). 

My question is: is there some way for me to make sure the button always appears and sends the user back to the right collection? Perhaps I can define the destination of the button based on a product's tags (I imagine they are stored in some array that is accessible)? Or perhaps even parse the name of the collection from the product name, by following the folling naming scheme. Product P in Collection C name = "C P". If I could parse the first character, I would be able to return to that particular collection.

I might be overcomplicating this, but I am at a loss, and I am new to liquid!

Thank you!
Regards,
Jan

0 Likes
Alan15
Pathfinder
95 21 25

How about using the product.collections attribute to get the collections that the product belongs to and add the collection title to the button link url?

Obviously if the product belongs to more than one collection it would be more complicated, you'll get more than one title returned. 

{% for collection in product.collections %}
<a href="https://www.your-shop-name/{{ collection.title }}>Back to collection</a>
{% endfor %}

Alan15
Pathfinder
95 21 25

This is an accepted solution.

You can actually return the url, you don't need the title:

{% for collection in product.collections %}
<a href="{{ collection.url }}">Back to Collection</a>
{% endfor %}

vereinsshop
Tourist
7 0 0

I didnt think it would work with the array of collections, but it did! Thank you!

0 Likes