如何将Shopify搜索输入限制为特定的集合?

2022-06-28 00:00:00 shopify javascript shopify-template

我正在尝试进行仅按集合中的结果进行搜索/筛选的搜索输入。

这是搜索模板:

<div id="col-main" class="page-search">

{% if search.results_count == 0 or search.performed == false %}
  <h4 class="title">{{ 'search.general.heading' | t: count: search.results_count }}: {{ 'search.general.results_with_count' | t: terms: search.terms, count: search.results_count }}</h4>

  <form class="search-form" action="/search">
    <input type="hidden" name="type" value="product" />
    <input type="hidden" name="options[prefix]" value="last" />
    <input type="text" name="q" class="search_box" placeholder="{{settings.search_placeholder}}" value="{{ search.terms }}" />

    <button type="submit" class="search-submit" title="Search">
      {% if settings.search_icon_type == 'font-icon' %}
        <i class="demo-icon {{ settings.search_font_icon }}"></i>
      {% else %}
        <img src="{{ settings.search_icon | img_url: '20x' }}" alt="Search" />
      {% endif %}
    </button>
  </form>

{% else %}
  {% if search.performed %}
    {% paginate search.results by 16 %}

    <h4 class="title">{{ 'search.general.heading' | t: count: search.results_count }}: {{ 'search.general.results_with_count' | t: terms: search.terms, count: search.results_count }}</h4>

    <div class="cata-product cp-grid">
      {% for product in search.results %}
        {% if product.object_type == 'product' %}
          <div class="product-grid-item">
            {% include 'product-item' %}
          </div>
        {% else %}
          <div class="article-grid-item">
            {% include 'article-result' %}
          </div>
        {% endif %}
      {% endfor %}
    </div>

    {% if paginate.pages > 1 %}
      {% include 'pagination' %}
    {% endif %}

    {% endpaginate %}
  {% endif %}

{% endif %}

如何将其编辑为仅在与"已用"匹配的集合名称中进行搜索?

有shopify collection object,但不知道是否应该尝试编辑:

<input type="text" name="q" class="search_box" placeholder="{{settings.search_placeholder}}" value="{{ search.terms }}" />

或搜索结果

{% for product in search.results %}

如有任何帮助我们将不胜感激,谢谢


解决方案

获取所有结果、筛选、生成;问题是分页,我目前没有解决方案。

{% paginate search.results by search.results_count %}
  {% for item in search.results %}
    {% if item.object_type == 'product' %}
      {% for collection in item.collections %}
        {% if collection.title contains 'myCollectionTitle' %}
          {% assign image = item.featured_image | img_url: 'medium' %}
          
          <a href="{{item.url}}">{{item.title}} <img src="{{image}}" alt="" class="" loading="lazy" width="" height=""></a><br>
          {% endif %}
      {% endfor %}
    {% endif %}
  {% endfor %}  
{% endpaginate %}

相关文章