🛍️ 商品表示中級
商品バリアントの価格表示
選択中のバリアントの現在価格・比較価格(定価)・割引率を表示するブロック。セール状態では打ち消し線で定価を示し、割引額または割引率をバッジで表示する。在庫なしのバリアントは「売切」バッジを出す。
用途
商品詳細ページでバリアント(色・サイズ)を切り替えたときに、その時点の価格を動的に更新表示したいとき。
設置場所
snippets/product-block-price.liquid に配置し、sections/main-product.liquid または商品セクション内で `{% render 'product-block-price', product: product, block: block %}` で呼び出す。theme.liquid の設定スキーマに padding-top・padding-bottom・フォントサイズ・割引表示タイプ(金額 or パーセンテージ)を選択肢として追加する。
注意点
compare_at_price が設定されていないバリアントではセール判定が false になるため、Shopify 管理画面で各バリアントに定価を入力しておく前提。割引率計算時に compare_at_price が 0 以下だと除算エラーになるので、条件分岐で値の有効性を確認してから計算する。`money_without_trailing_zeros` フィルタは小数第2位が 0 なら省略するため、価格表示が揺らぐことがある(例:1000.00 → 1000、1000.50 → 1000.5)。
コード
55 行 / liquid{% liquid
assign current_variant = product.selected_or_first_available_variant
assign pt = block.settings.pt | prepend: 'pt-'
assign pb = block.settings.pb | prepend: 'pb-'
%}
<div
class="product-price {{ block.settings.font_size }} {{ pt }} {{ pb }}"
{{ block.shopify_attributes }}>
<div class="d-flex align-items-center">
<s
class="product-price-compare"
{% unless current_variant.compare_at_price > current_variant.price %}hidden{% endunless %}>
<span class="visually-hidden">
{{ 'product.regular_price' | t }}
</span>
{{ current_variant.compare_at_price | money_without_trailing_zeros }}
</s>
<span class="product-price-final {% if current_variant.compare_at_price > current_variant.price %}product-price-final-sale{% endif %}">
{% if current_variant.compare_at_price > current_variant.price %}
<span class="visually-hidden">
{{ 'product.sale_price' | t }}
</span>
{% endif %}
{{ current_variant.price | money_without_trailing_zeros }}
</span>
<span
class="product-sale-badge badge"
data-discount-type="{{ block.settings.badge_discount_type }}"
{% unless current_variant.compare_at_price > current_variant.price %}hidden{% endunless %}>
{{ 'product.save' | t }}
{% if current_variant.compare_at_price > 0 %}
{% if block.settings.badge_discount_type == 'percentage' %}
{{ current_variant.compare_at_price | minus: current_variant.price | times: 100 | divided_by: current_variant.compare_at_price | round | append: '%' }}
{% else %}
{{ current_variant.compare_at_price | minus: current_variant.price | money_without_trailing_zeros }}
{% endif %}
{% endif %}
</span>
<span
class="product-sold-out-badge badge"
{% if current_variant.available %}hidden{% endif %}>
{{ 'product.sold_out' | t }}
</span>
</div>
</div>
{{ form | payment_terms }}出典・ライセンス
- 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 行