简体   繁体   中英

Related Products Shopify Liquid

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.

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 %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM