Tags Shopify中的标签相关产品

Tags Shopify中的标签相关产品,tags,shopify,liquid,product,Tags,Shopify,Liquid,Product,我构建了一个液体模板,将当前产品的标签与商店中所有其他产品的标签进行比较,并在页面底部显示四种相关产品 这是可行的,但我认为我做得相当低效。有没有办法让这项工作做得更好一点 {% if settings.products_per_row == "4" %} {% assign number_of_related_products_to_show = 4 %} {% elsif settings.products_per_row == "3" %} {% assign number_of_rela

我构建了一个液体模板,将当前产品的标签与商店中所有其他产品的标签进行比较,并在页面底部显示四种相关产品

这是可行的,但我认为我做得相当低效。有没有办法让这项工作做得更好一点

{% if settings.products_per_row == "4" %}
{% assign number_of_related_products_to_show = 4 %}
{% elsif settings.products_per_row == "3" %}
{% assign number_of_related_products_to_show = 3 %}
{% else %}
{% assign number_of_related_products_to_show = 2 %}
{% endif %}

{% assign number_of_related_products_to_fetch = number_of_related_products_to_show | plus: 1 %}
{% assign current_product_tags = product.tags %}

{% for c in collections %}
    {% if c.handle == 'all' %}
        {% assign collection_all = c %}
    {% endif %}
{% endfor %}

{% assign found_first_match = false %}
{% assign found_second_match = false %}
{% paginate collection_all.products by 1000 %}
{% for product in collection_all.products %}
{% for tag in product.tags %}
    {% if current_product_tags contains tag and found_first_match == false and tag != 'Made in USA' %}
        {% assign found_first_match = true %}
        {% assign first_match = tag %}
    {% endif %}
    {% if current_product_tags contains tag and found_first_match == true and tag != first_match and tag != 'Made in USA' %}
        {% assign found_second_match = true %}
        {% assign second_match = tag %}
    {% endif %}
{% endfor %}
{% endfor %}
{% endpaginate %}

{% assign matches_found = false %}
{% assign current_product = product %}
{% assign current_product_found = false %}

{% paginate collection_all.products by 1000 %}
{% for product in collection_all.products %}
  {% if product.handle == current_product.handle %}
    {% assign current_product_found = true %}
  {% else %}
    {% if product.tags contains first_match %}
      {% unless current_product_found == false and forloop.last %}
        {% assign matches_found = true %}
      {% endunless %}
    {% endif %}
    {% if product.tags contains second_match and matches_found == false  %}
      {% unless current_product_found == false and forloop.last %}
        {% assign matches_found = true %}
      {% endunless %}
    {% endif %}
  {% endif %}
{% endfor %}
{% endpaginate %}

{% if matches_found == true %}

<div class="row">
  <div class="span12">
    <h3 class="collection-title">Related products</h3>
  </div>
</div> 

<div class="row products">

{% paginate collection_all.products by 1000 %}
{% for product in collection_all.products %}
  {% if product.handle == current_product.handle %}
    {% assign current_product_found = true %}
  {% else %}
    {% if product.tags contains first_match %}
      {% unless current_product_found == false and forloop.last %}
        {% include 'related-product-loop' with collection.handle %}
        {% assign matched_product = product.title %}
      {% endunless %}
    {% endif %}
    {% if product.tags contains second_match %}
      {% unless current_product_found == false and forloop.last or matched_product == product.title %}
        {% include 'related-product-loop' with collection.handle %}
      {% endunless %}
    {% endif %}
  {% endif %}
{% endfor %}
{% endpaginate %}

</div>

{% endif %}

{{ 'jquery.pick.js' | asset_url | script_tag }}
<script type="text/javascript" charset="utf-8">
//<![CDATA[
var howMany = {{ number_of_related_products_to_show }};
jQuery(function() {
  jQuery('.products .product').pick(howMany);
});
//]]>
</script>
{%if settings.products\u per\u row==“4”}
{%将与\u相关的\u产品的数量\u分配给\u show=4%}
{%elsif settings.products_每_行==“3”%}
{%将与\u相关的\u产品的数量\u分配给\u show=3%}
{%else%}
{%将与\u相关的\u产品的数量\u分配给\u show=2%}
{%endif%}
{%分配数量_的_相关的_产品_到_fetch=数量_的_相关的_产品_到|显示|加:1%}
{%assign current_product_tags=product.tags%}
{集合%%中的c为%0}
{%c.handle='all%}
{%assign collection_all=c%}
{%endif%}
{%endfor%}
{%assign found_first_match=false%}
{%assign found_second_match=false%}
{%paginate collection_all.products乘以1000%}
{集合_all.products%}中产品的%
{product.tags%中的标记的%s}
{%如果当前的\u产品\u标记包含标记并找到了\u first\u match==false和tag!=“美国制造”%}
{%assign found_first_match=true%}
{%assign first_match=tag%}
{%endif%}
{%如果当前的\u产品\u标记包含标记并找到\u first\u match==true和tag!=first\u match和tag!='美国制造'%}
{%assign found_second_match=true%}
{%assign second_match=tag%}
{%endif%}
{%endfor%}
{%endfor%}
{%endpaginate%}
{%assign matches_found=false%}
{%assign current_product=product%}
{%assign current_product_found=false%}
{%paginate collection_all.products乘以1000%}
{集合_all.products%}中产品的%
{%if product.handle==当前_product.handle%}
{%assign current_product_found=true%}
{%else%}
{%如果product.tags包含第一个匹配项%}
{%除非找到当前的_产品==false和forloop.last%}
{%assign matches_found=true%}
{%end除非%}
{%endif%}
{%if product.tags包含第二个\u匹配项和匹配项\u found==false%}
{%除非找到当前的_产品==false和forloop.last%}
{%assign matches_found=true%}
{%end除非%}
{%endif%}
{%endif%}
{%endfor%}
{%endpaginate%}
{%if匹配项_find==true%}
相关产品
{%paginate collection_all.products乘以1000%}
{集合_all.products%}中产品的%
{%if product.handle==当前_product.handle%}
{%assign current_product_found=true%}
{%else%}
{%如果product.tags包含第一个匹配项%}
{%除非找到当前的_产品==false和forloop.last%}
{%include'related product loop'与collection.handle%}
{%assign matched_product=product.title%}
{%end除非%}
{%endif%}
{%如果product.tags包含第二个匹配项%}
{%除非当前_-product_-found==false和forloop.last或匹配的_-product==product.title%}
{%include'related product loop'与collection.handle%}
{%end除非%}
{%endif%}
{%endif%}
{%endfor%}
{%endpaginate%}
{%endif%}
{{'jquery.pick.js'|资产| url |脚本(标记)}
//
我使用jquery.pick.js随机显示四种产品


想法?

我建议在Shopify wiki上看一看这篇文章:

也许第节中使用的方法将是实现相关产品的更干净的方法。然而,如果您需要使用产品标签,在第节中也有关于如何使用的说明

编辑:也许您的代码可以简化成这样。它与上面的内容非常相似,但只是将其简化为一个循环,而不是三个循环

{% if settings.products_per_row == "3" or settings.products_per_row == "4" %}
  {% assign number_of_related_products_to_show = settings.products_per_row | times: 1 %}
{% else %}
  {% assign number_of_related_products_to_show = 2 %}
{% endif %}

{% 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 and tag != 'Made in USA' %}
        {% if found_first_match == false %}
          {% assign found_first_match = true %}
          {% assign first_match = tag %}
        {% elsif found_second_match == false %}
          {% 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 %}

        <div class="row">
          <div class="span12">
            <h3 class="collection-title">Related products</h3>
          </div>
        </div>
        <div class="row products">

      {% endif %}
      {% if product.tags contains first_match or product.tags contains second_match %}
        {% include 'related-product-loop' with collection.handle %}
      {% endif %}
    {% endif %}
  {% endunless %}
{% endfor %}
{% if first_related_product == false %} </div> {% endif %}
{% endpaginate %}

{{ 'jquery.pick.js' | asset_url | script_tag }}
<script type="text/javascript" charset="utf-8">
//<![CDATA[
  var howMany = {{ number_of_related_products_to_show }};
  jQuery(function() {
    jQuery('.products .product').pick(howMany);
  });
//]]>
</script>
{%if settings.products\u per\u row==“3”或settings.products\u per\u row==“4”}
{%将与_相关的_产品的数量_分配给_show=settings.products_每行|次:1%}
{%else%}
{%将与\u相关的\u产品的数量\u分配给\u show=2%}
{%endif%}
{%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%}
{%1000%分页集合.all.products}
{collections.all.products%中的产品的%s}
{%除非product.handle==当前_product.handle%}
{product.tags%中的标记的%s}
{%如果当前的_产品_标签包含标签和标签!=“美国制造”%}
{%if find_first_match==false%}
{%assign found_first_match=true%}
{%assign first_match=tag%}
{%elsif找到第二个匹配项==false%}
{%assign found_second_match=true%}
{%assign second_match=tag%}
{%endif%}
{%endif%}
{%endfor%}
{%if find_first_match==true%}
{%如果第一个与\u相关的\u产品==true%}
{%assign first_related_product==false%}
相关产品
{%endif%}
{%如果product.tags包含第一个匹配项或product.tags包含第二个匹配项%}
{%include'related product loop'与collection.handle%}
{%endif%}
{%endif%}
{%end除非%}
{%endfor%}
{%if第一个与_相关的_产品==false%}{%endif%}
{%endpaginate%}
{{'jquery.pick.js'|资产| url |脚本(标记)}
//

这段代码是一个要点。我包括了2个文件,第二个选项在产品中使用了2个循环,但可能更可读一些。(我无法在两者之间做出选择,因此我将两者都包括在内。)

我感谢您的回复。我实际上看到了这两种情况(此代码实际上基于相关的收集解决方案),但客户希望它使用他们已经设置的现有标记系统,因此该文档中的标记解决方案被证明是不可用的/@约翰巴克我编辑了我的答案。。。也许你需要做的就是整理你的房间