shopify利用模版二次过滤实现对sku的精准搜索

运营反馈 当她在前台输入一个sku进行搜索的时候,会出现多个非这个sku  的结果,还得人工二次识别,很麻烦,希望能解决搜索sku不精准的问题

企业微信截图_17828711672200

 

以下是解决的办法(针对impulse 模版)

 

1,搜索页面的调用模版的顺序

sections/main-search.liquid->collection-grid.liquid

/search?q=517452302
→ main-search.liquid
→ collection-grid.liquid
→ 显示 9个商品
 

Shopify 内置对象:

  • search.performed:是否执行了搜索
  • search.terms:用户输入的关键词
  • search.results:搜索结果数组
  • search.results_count:结果总数(Shopify 原始数量,仍是 9)

思路:shopify 搜索api 返回了9个模糊搜索的商品,在显示商品的时候,对这9个商品逐个判断一下sku是否等于517452302,不等于就continue , 不显示

 

具体修改代码

增加用于判断当前一个商品sku是否等于搜索sku的 snippets/search-exact-sku-match.liquid

{% comment %}
  Check if a product has a variant with an exact SKU match.
  @param product {product}
  @param sku_query {string}
  Outputs: true or false
{% endcomment %}
{% liquid
  assign sku_match = false
  assign product_variants = product.variants

  if product_variants.size == 0 and product.handle != blank
    assign full_product = all_products[product.handle]
    if full_product != blank
      assign product_variants = full_product.variants
    endif
  endif

  for variant in product_variants
    if variant.sku == sku_query
      assign sku_match = true
      break
    endif
  endfor
%}
{%- if sku_match -%}true{%- else -%}false{%- endif -%}

    循环搜索返回结果来调上面"search-exact-sku-match.liquid"的snippets/search-exact-sku-detect.liquid ,用来对搜索返回结果进行逐个判断sku是否和搜索的关键词一致

{% comment %}
  Detect whether exact SKU filtering should be applied.
  Only activates for pure-numeric queries with at least one exact variant match.
  @param terms {string}
  @param results {array}
  Outputs: active|count|sku_query (pipe-separated)
{% endcomment %}
{% liquid
  assign sku_query = terms | strip
  assign sku_query_numeric = sku_query | plus: 0 | append: ''
  assign filter_active = false
  assign match_count = 0

  if sku_query != blank and sku_query == sku_query_numeric
    for item in results
      if item.object_type == 'product' or item.object_type == blank
        capture item_match
          render 'search-exact-sku-match', product: item, sku_query: sku_query
        endcapture
        assign item_match = item_match | strip
        if item_match == 'true'
          assign filter_active = true
          assign match_count = match_count | plus: 1
        endif
      endif
    endfor
  endif
%}
{% if filter_active %}true{% else %}false{% endif %}|{{ match_count }}|{{ sku_query }}

 现在开始对搜索使用到的模版主模版main-search.liquid,和渲染返回的产品集的collection-grid.liquid 使用上面经过匹配过的含有是否和搜索关键词匹配的sku一致的结果,不一致的sku就跳过,不作显示(如果不是搜索结果没有一个sku和搜索的关键词匹配则不作过滤)

修改 

image

     {%- capture sku_state -%}
        {%- render 'search-exact-sku-detect', terms: search.terms, results: search.results -%}
      {%- endcapture -%}
      {%- assign sku_state_parts = sku_state | strip | split: '|' -%}
      {%- assign exact_sku_filter = false -%}
      {%- if sku_state_parts[0] == 'true' -%}
        {%- assign exact_sku_filter = true -%}
      {%- endif -%}
      {%- assign exact_sku_count = sku_state_parts[1] | plus: 0 -%}
      {%- assign sku_query = sku_state_parts[2] -%}

 

 

image

              exact_sku_filter: exact_sku_filter,
              sku_query: sku_query,
              exact_sku_count: exact_sku_count

  

image

 

image

 

 

posted @ 2026-07-02 13:42  i金少  阅读(8)  评论(0)    收藏  举报