需要帮助通过标签而不是收藏来显示相关产品

时间:2019-05-02 22:27:20

标签: shopify liquid

我在一个集合中有数百种产品,我想用相似的标签对那个集合中的项目进行分组

如何在相关滑块中显示所有带有匹配标签的产品?

这是到目前为止我尝试过的,我设法将集合中的所有相关产品归为一类。

{% if settings.show_related_product %}
  <div id="related_item" class="home-carousel">

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

      {% if settings.heading_related_product != blank %}
      <h4>{{ settings.heading_related_product }}</h4>
      {% endif %}

      <div class="related-items">
        {% assign current_product = product %}
        {% assign current_product_found = false %}
        {% for product in collection.products limit: settings.related_product_number %}
          {% if product.handle == current_product.handle %}
            {% assign current_product_found = true %}
          {% else %}
            {% unless current_product_found == false and forloop.last %}
            <div class="related-item">
              {% include 'product-item' with collection.handle %}
            </div>
            {% endunless %}
          {% endif %}
        {% endfor %}

      </div>
    {% endif %}
  </div>
{% endif %}

{% if settings.show_wear_with and settings.wear_with_col != blank%}
  <div id="wear_with_item" class="home-carousel">


    {% assign wearwithCol = collections[settings.wear_with_col] %}
    {% assign wearwithNum = settings.wear_with_number %}

    {% if settings.heading_wear_with != blank %}
    <h4>{{ settings.heading_wear_with }}</h4>
    {% endif %}

    <div class="wear-with-items">
      {% for product in wearwithCol.products limit: wearwithNum %}

      <div class="wear-with-item">
        {% include 'product-item' %}
      </div>

      {% endfor %}
    </div>

  </div>
{% endif %}

1 个答案:

答案 0 :(得分:0)

当您需要按标签过滤产品时,最好的解决方案是使用JS,因为液体没有原生方法。

所以最好的方法是定位/collections/all/TAG页,并通过AJAX请求从那里获得产品,因为产品将被该页面上的标签过滤。

是这样的:

fetch('/collections/all/SOME_TAG')
  .then(r => r.json())
  .then(data => /* Do Something With The data */)

如果您真的真的只想使用流动代码,则可以执行以下“ hack”操作:

{% paginate collections.all.products by 9999 %}
 {% for _product in collections.all.products %}
   {% if _product.tags contains 'SOME_TAG' %}
     Do something here
   {% endif %}
 {% endfor %}
{% endpaginate %}

但是我不推荐仅使用液体的解决方案,因为这会使服务器时间负载显着增加。