我有一个"相关产品"我的产品页面上的部分。现在它显示了与#34;集合相关的产品"。是否可以显示具有相同标签的相关产品?
这是我的 related-products.liquid 代码。
感谢。
{% assign number_of_products = 4 %}
{% assign number_of_products_to_fetch = number_of_products | plus: 1 %}
{% if collection == null or collection.handle == 'frontpage' or collection.handle == 'all' %}
{% assign found_a_collection = false %}
{% for c in product.collections %}
{% if found_a_collection == false and c.handle != 'frontpage' and c.handle != 'all' and c.all_products_count > 1 %}
{% assign found_a_collection = true %}
{% assign collection = c %}
{% endif %}
{% endfor %}
{% endif %}
{% if collection and collection.products_count > 1 %}
<div class="related">
<h1>You Might Also Like</h1>
<div class="products clearfix">
{% assign current_product = product %}
{% assign current_product_found = false %}
{% for product in collection.products limit: number_of_products_to_fetch %}
{% if product.handle == current_product.handle %}
{% assign current_product_found = true %}
{% else %}
{% unless current_product_found == false and forloop.last %}
<li>
<a href="{{ product.url | within: collection }}" class="product__image" title="{{ product.title | escape }}">
<img src="{{ product.featured_image.src | img_url: '350x350' }}" alt="{{ product.featured_image.alt | escape }}">
</a>
</li>
{% endunless %}
{% endif %}
{% endfor %}
</div>
</div>
{% endif %}
答案 0 :(得分:0)
让我们更改遍历项目集合的for循环,并忽略未标记的项目。
{% for product in collection.products limit: number_of_products_to_fetch %}
{% if product.handle == current_product.handle %}
{% assign current_product_found = true %}
{% else %}
{% unless current_product_found == false and forloop.last %}
{% if current_product_found.tags contains 'best-tag-ever' %}
<li>
<a href="{{ product.url | within: collection }}" class="product__image" title="{{ product.title | escape }}">
<img src="{{ product.featured_image.src | img_url: '350x350' }}" alt="{{ product.featured_image.alt | escape }}">
</a>
</li>
{% endif %}
{% endunless %}
{% endif %}
{% endfor %}
请注意!这仍然只会遍历提供的集合。这意味着将出现的产品需要a)包含在此类集合中,并且b)标记为'best-tag-ever'
。如果您想展示整个商店的产品而不是所述产品系列,您可以将该系列换成包含商店中每件商品的all
系列。
答案 1 :(得分:0)
使用标签 -
检查以下代码以添加相关产品{% assign number_of_related_products_to_show = 2 %}
{% assign current_product = product %} {% assign current_product_tags = product.tags %}
{% assign found_first_match = false %} {% assign found_second_match = false %}
{% assign first_related_product = true %}
{% paginate collections.all.products by 1000 %}
{% for product in collections.all.products %}
{% unless product.handle == current_product.handle %}
{% for tag in product.tags %}
{% if current_product_tags contains tag %}
{% if found_first_match == false %}
{% assign found_first_match = true %}
{% assign first_match = tag %}
{% else %}
{% assign found_second_match = true %}
{% assign second_match = tag %}
{% endif %}
{% endif %}
{% endfor %}
{% if found_first_match == true %}
{% if first_related_product == true %}
{% assign first_related_product = false %}
Related products
{% endif %}
{% if product.tags contains first_match or product.tags contains second_match %}
{% include 'product-grid-item' with collection.handle %}
{% endif %}
{% endif %}
{% endunless %}
{% endfor %}
{% if first_related_product == false %}
{% endif %}
{% endpaginate %}