🔍 コレクション/検索中級
予測検索結果の表示
検索入力時にリアルタイムで商品、コレクション、記事、ページを検索し、その結果を絞り込み可能なリスト形式で表示するセクション。検索クエリに応じてフィルタ対象とする情報源を管理画面で切り替えられる。
用途
ヘッダーの検索ボックス直下に、入力と同時に商品やコレクションのサジェスト結果を表示したいとき。ユーザーが検索結果ページに遷移する前に目的の商品を見つけやすくする。
設置場所
sections/predictive-search.liquid に配置し、theme.liquid または header セクション内の検索入力フォーム直後に `{% section 'predictive-search' %}` で呼び出す。または JavaScript の Fetch リクエスト後、動的に DOM に挿入する。
注意点
このセクションは Shopify の `predictive_search` オブジェクトの生成に依存するため、検索入力欄に `data-predictive-search-endpoint` 属性が設定されていることが前提となる。管理画面の「オンラインストア > 設定 > 検索」で「予測検索を表示」が有効化されていないと動作しない。product.featured_image が null の商品は画像が表示されないため、全商品に代替画像を用意しておくと良い。aria-selected や role="option" 属性が指定されているため、キーボード操作(上下キーでリスト移動)には別途 JavaScript イベントリスナーを実装する必要がある。
コード
70 行 / liquid{%- if predictive_search.performed -%}
{% liquid
assign results = predictive_search.resources.products.size
if settings.search_collections
assign results = results | plus: predictive_search.resources.collections.size
endif
if settings.search_articles
assign results = results | plus: predictive_search.resources.articles.size
endif
if settings.search_pages
assign results = results | plus: predictive_search.resources.pages.size
endif
%}
<div
id="predictive-search-results"
class="predictive-search-results pt-7"
data-results-text="{{ 'general.search.results' | t: count: results }}">
{%- if predictive_search.resources.products.size > 0 -%}
<h3
id="predictive-search-products-title"
class="title title-border-bottom h6 text-center mb-4">
<span>{{- 'general.search.products' | t }} ({{- predictive_search.resources.products.size -}})</span>
</h3>
<ul
id="predictive-search-results-products-list"
class="predictive-search-results-products-list list-group list-group-flush list-unstyled mb-6"
role="group"
aria-labelledby="predictive-search-products-title">
{%- for product in predictive_search.resources.products -%}
<li
id="predictive-search-option-product-{{ forloop.index }}"
class="list-group-item"
role="option"
aria-selected="false">
<a
href="{{ product.url }}"
class="list-group-item-action"
tabindex="-1">
<div class="row mx-n3 align-items-center">
<div class="col-6 px-3">
{% render 'image-url', img: product.featured_image, orientation: settings.product_card_img_orientation, class: settings.product_card_img_border %}
</div>
<div class="col-12 px-3">
<h3 class="cart-item-product-title h6 mb-1">
{{ product.title }}
</h3>
{% render 'product-rating-badge', product: product %}
{% render 'product-card-price', product: product %}
</div>
</div>
</a>
</li>
{%- endfor -%}
</ul>
{%- endif -%}
<div
id="predictive-search-option-search-keywords"
class="predictive-search-option-search-keywords">
<button
class="btn btn-outline-primary w-100 d-flex align-items-center justify-content-between"
tabindex="-1"
role="option"
aria-selected="false">
{{- 'general.search.search_for' | t: terms: predictive_search.terms -}}
{% render 'svg-icons', icon: 'arrow-right', size: 22 %}
</button>
</div>
</div>
{%- endif -%}出典・ライセンス
- Repository:
- https://github.com/kondasoft/ks-bootshop
- License:
- MIT
このコードは kondasoft 著作の MIT ライセンスソースです。 原本の著作権は kondasoft が保有します。日本語訳は ALSEL によるものです。
関連項目
🔍 コレクション/検索初級
ページネーションの表示
ページネーションUI(前へ・次へ、ページ番号)を paginate オブジェクトから描画するスニペット。デフォルトのページネーション形式で複数ページに分割したコンテンツをナビゲートできる。
📁 shopify-code-snippets·MIT·6 行
🔍 コレクション/検索初級
ページネーションの現在位置表示
コレクションや検索結果の一覧ページで、現在のページ番号と全ページ数を表示する。Shopify の多言語対応翻訳キーを使用して、言語ごとに自動で表記を切り替える。
📁 shopify-code-snippets·MIT·6 行
🔍 コレクション/検索中級
検索結果のペジネーション比較
検索結果を paginate タグで複数パターン分割して表示するテンプレート。デフォルトペジネーションと window_size:1 オプション指定の両方の書き方を示しており、ページネーション動作の違いを検証するサンプルコード。
📁 theme-tools·MIT·12 行
🔍 コレクション/検索初級
検索結果のページネーション設定
検索結果を paginate タグでページ分割し、複数の window_size 設定を並べて出力する方法。28件単位のペーグネーションと、ウィンドウサイズを明示的に指定した設定の2パターンを示している。
📁 theme-tools·MIT·12 行
🔍 コレクション/検索初級
コレクションページの商品ギャラリー
コレクション内の商品を3列のテーブルレイアウトで表示するテンプレート。各商品にサムネイル、タイトル、価格(セール時は定価を取消線)を表示し、ページネーションに対応する。
📁 liquid·MIT·20 行
🔍 コレクション/検索初級
コレクションページの商品一覧
コレクション内の商品を一覧表示し、商品画像・タイトル・説明文・価格帯をカード状に並べる。ページネーション機能で20件ごとに分割表示する。
📁 liquid·MIT·23 行