Liquid Snippets by ALSEL
🛍️ 商品表示中級

商品カードの追加・バリアント選択フォーム

商品カード内で「カートに追加」ボタンを表示するスニペット。単一バリアントの商品は直接購入可能で、複数バリアント商品はドロップダウンで選択してから追加する。

用途
コレクションページや関連商品、検索結果の商品カードで、カード内から直接カートに商品を追加したいとき。
設置場所
snippets/product-card-form.liquid に配置し、product-card.liquid またはコレクション/検索テンプレート内の `{% render 'product-card-form', product: product %}` で呼び出す。btn_color パラメータでボタン色を上書きできる。
注意点
form 要素は `<product-form>` カスタムタグでラップされているため、カート追加時の動作を制御する JavaScript(商品情報の送信、カート更新など)が別途必要。バリアント選択時に在庫状態(disabled)が反映されるよう、variant.available が正しく設定されていることを確認する。ボタンの色は settings.product_card_atc_btn_color(テーマ設定)がデフォルトで、render 時に btn_color パラメータで Bootstrap のクラス名(例:'btn-primary')を指定して上書きできる。
タグ:productformvariantcartadd-to-cart

コード

46 行 / liquid
{% comment %}
  Renders an "Add to cart" button or a "Quick view" button if the product has multiple variants
  Usually it is used with the product card

  Accepts:
  - product: {object} The product object
  - btn_color {string} (Optional) The button color class (bootstrap) to apply to the button

  Usage:
  {% render 'product-card-form' product: product  %}
{% endcomment %}

{% liquid 
  assign btn_color = btn_color | default: settings.product_card_atc_btn_color
%}

<product-form>
  {% form 'product', product, data-productid: product.id %}
    {% if product.has_only_default_variant %}
      <input type="hidden" name="id" data-productid="{{ product.id }}" value="{{ product.first_available_variant.id }}">
    {% else %}
      <select 
        class="form-select form-select-sm mb-3"
        name="id" 
        aria-label="{{ 'product.select_variant' | t }}">
        {% for variant in product.variants %}
          <option value="{{ variant.id }}" {% unless variant.available %}disabled{% endunless %}>
            {{ variant.title }} - {{ variant.price | money_without_trailing_zeros }}
          </option>
        {% endfor %}
      </select>
    {% endif %}
    <button
      class="btn btn-atc btn-sm {{ btn_color }}"
      type="submit"
      name="add"
      {% unless product.available %}disabled{% endunless %}>
      {% if product.available %}
        {{ 'product.add_to_cart' | t }}
      {% else %}
        {{ 'product.sold_out' | t }}
      {% endif %}
    </button>
  {% endform %}
</product-form>

出典・ライセンス

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