🛍️ 商品表示上級
商品ギャラリーのSwiper実装
Swiper ライブラリを使用した商品メディアギャラリー。メイン画像をスライドで表示し、サムネイルで操作できる。画像とビデオの両方に対応。
用途
商品詳細ページでメイン画像をスライダー表示し、下部のサムネイルで前後の画像に切り替えるとき。
設置場所
snippets/product-block-media-gallery.liquid に配置し、templates/product.liquid または sections/main-product.liquid 内で `{% render 'product-block-media-gallery', product: product, section: section %}` で呼び出す。
注意点
block.settings で speed・thumbs_per_view・controls_color・img_orientation・show_thumbs が定義されている必要があり、セクション JSON に対応する設定が整備されていることが前提。Swiper JavaScript ライブラリと `product-media-gallery` カスタム要素が別途読み込まれていないと動作しないため、theme.liquid の <head> に Swiper CDN または JS ファイルが含まれているか確認する。3枚目以降のメディアは遅延読み込み(lazy)が適用されるため、モバイル通信環境での読み込み速度改善に有効だが、画像サイズ 800px は設定で調整可能。
コード
76 行 / liquid{% liquid
assign block = section.blocks | where: 'type', 'media_gallery' | first
assign current_variant = product.selected_or_first_available_variant
if current_variant.featured_image
assign initial_slide = product.selected_or_first_available_variant.featured_media.position | minus: 1
else
assign initial_slide = 0
endif
%}
{% if product == nil %}
{{ 'image' | placeholder_svg_tag: 'img-thumbnail' }}
{% elsif block != nil %}
<product-media-gallery
class="product-media-gallery"
data-product-id="{{ product.id }}"
data-initial-slide="{{ initial_slide }}"
data-speed="{{ block.settings.speed }}"
data-thumbs-per-view="{{ block.settings.thumbs_per_view }}">
<div class="swiper-main-wrapper {{ block.settings.img_border }}">
<div
class="swiper-main swiper"
style="--swiper-theme-color: {{ block.settings.controls_color | prepend: 'var(--bs-' | append: '-rgb)' }}">
<div class="swiper-wrapper">
{% for media in product.media %}
{% liquid
assign media_loading = 'eager'
if forloop.index > 2
assign media_loading = 'lazy'
endif
%}
<div class="swiper-slide" lazy="{% if media_loading == 'lazy' %}true{% endif %}">
{% case media.media_type %}
{% when 'image' %}
{% render 'image-url', img: media, size: 800, orientation: block.settings.img_orientation, loading: media_loading, class: '' %}
{% when 'video' %}
{{ media | video_tag: controls: true, image_size: '800x', loading: media_loading, class: 'bg-black w-100' }}
{% endcase %}
</div>
{% endfor %}
</div>
<div class="swiper-controls">
<div class="swiper-pagination"></div>
<div class="swiper-scrollbar"></div>
<div class="swiper-button-prev">
{% render 'svg-icons', icon: 'chevron-left', size: 24 %}
</div>
<div class="swiper-button-next">
{% render 'svg-icons', icon: 'chevron-right', size: 24 %}
</div>
</div>
</div>
</div>
<div
class="swiper-thumbs swiper mt-4"
data-show-thumbs="{{ block.settings.show_thumbs }}"
data-controls-color="{{ block.settings.controls_color }}"
{% if product.media.size < 2 %}hidden{% endif %}>
<div class="swiper-wrapper">
{% for media in product.media %}
<div class="swiper-slide" data-media-type="{{ media.media_type }}">
{% case media.media_type %}
{% when 'image' %}
{% render 'image-url', img: media, size: 800, orientation: settings.product_img_orientation, class: 'img-thumbnail' %}
{% when 'video' %}
{% render 'svg-icons', icon: 'play', size: '18', aria-hidden: 'true' %}
{% render 'image-url', img: media.preview_image, size: 800, orientation: settings.product_img_orientation, class: 'img-thumbnail' %}
{% endcase %}
</div>
{% endfor %}
</div>
</div>
</product-media-gallery>
{% endif %}出典・ライセンス
- Repository:
- https://github.com/kondasoft/ks-bootshop
- License:
- MIT
このコードは kondasoft 著作の MIT ライセンスソースです。 原本の著作権は kondasoft が保有します。日本語訳は ALSEL によるものです。
関連項目
🛍️ 商品表示上級
商品ブロック内のLiquid実行
セクションブロック内で管理画面から入力した Liquid コードを直接実行し、パディング設定で上下の余白を制御するスニペット。テーマカスタマイザーでコード記述が可能。
📁 ks-bootshop·MIT·8 行
🛍️ 商品表示初級
商品ページの見出し
商品詳細ページの商品名を h1 見出しで表示するブロック。テーマカスタマイザーから見出しサイズと上下の余白(padding)を調整できる。
📁 ks-bootshop·MIT·8 行
🛍️ 商品表示中級
商品レビューバッジの表示
Loox と JudgeMe 両方のレビュー集約アプリに対応したレーティングバッジを商品ページに埋め込む。メタフィールドから平均評価とレビュー数を取得し、各アプリの UI コンポーネントを描画する。
📁 ks-bootshop·MIT·9 行
🛍️ 商品表示中級
定期販売プランのデータ属性を動的に生成
商品の定期販売プラン(Selling Plans)をループで取得し、data 属性として HTML に埋め込むスニペット。JavaScript で定期販売オプションを制御するためのデータ基盤として機能する。
📁 theme-tools·MIT·10 行
🛍️ 商品表示初級
商品ページの区切り線
商品詳細ページで、セクション間に区切り線を挿入するブロック。パディング、高さ、背景色、透明度をカスタマイズできる。
📁 ks-bootshop·MIT·15 行
🛍️ 商品表示初級
商品画像のプリロード出力
商品オブジェクトから image_tag フィルターを使い、preload 属性付きで画像を出力するスニペット。複数商品のループ内でも使える基本的なパターン。
📁 theme-tools·MIT·16 行