2
votes

I'm trying to determine the correct Shopify Liquid syntax for outputting a list of products that match the same tag as the current product.

This is to appear in a "Related Products" box on the product page, and I'd like it only to list other products that match the same tag of the current product page.

Unfortunately the Related Products wiki page didn't help me with this.

1
A Product can have multiple Tags. Are you trying to match any Tags or a specific Tag?Rick Davies
Actually it is not a good idea to show related products just based on tags. You should use past order data, tags, collections and many other factors to select related products that your customers will be interested in. It is not possible (because of performance issues) to implement such advanced algorithms to select related products by just using liquid template engine. I developed Recomify Related Products App ( apps.shopify.com/recomify ) for Shopify to make all this work easy, fast and absolutely automatic.Farid Movsumov
@FeridMovsumov Stop spamming and advertising your app!Mohamed Anis Dahmani

1 Answers

1
votes

I'm not sure you can get a set of all products with a common tag (although I may be wrong) but here's a possible alternative way to approach it - create a smart collection of products that contain that tag, then output the products from that collection in the related items area.

To connect the product tag to the right collection on the product page, make sure that your collection handle is the same as the tag you're using, then do something like this to grab the right collection based on the tag.

{% for c in collections %}
  {% assign t = {{product.tags[0] | handleize}} %}
  {% if c.handle == t %}
    {% assign collection = c %}
  {% endif %} 
{% endfor %}

Then just output the products in the collection using the approach outlined in the wiki article you linked.

Something like this (assuming you use a "product-loop" include approach) should do the trick:

{% assign current_product = product %}
{% assign current_product_found = false %}
{% for product in collection.products %}
  {% if product.handle == current_product.handle %}
    {% assign current_product_found = true %}
  {% else %}
    {% unless current_product_found == false and forloop.last %}
      {% include 'product-loop' with collection.handle %}
    {% endunless %}
  {% endif %}
{% endfor %}