Liquid Snippets by ALSEL
📄 ページテンプレート中級

商品詳細ページのレイアウト

商品の画像、情報、バリアント選択、カート追加機能を一つのページに配置する従来型テンプレート。バリアント選択時に在庫状態と価格をリアルタイム更新する。

用途
Shopify の初期テンプレートを参考にした商品詳細ページの構築。複数バリアント(色・サイズ等)を扱い、選択時に価格と在庫状態を動的に切り替えたい場合。
設置場所
templates/product.liquid として配置し、ストア共通のヘッダー・フッターを layout/theme.liquid に分離した構成で使用する。JavaScript は Mootools ライブラリと Shopify.OptionSelectors に依存するため、theme.liquid で同ライブラリの読み込みが前提。
注意点
このコードは Mootools(古いフロントエンドフレームワーク)と Shopify 初期の OptionSelectors API に依存するため、現代の Shopify テーマ(Online Store 2.0 以降)では動作しない。product_img_url フィルターのサイズ引数('large'、'medium'、'small')はストアのアセット設定に応じて調整が必要。バリアント画像が複数ある場合、このテンプレートでは全画像を一覧表示するため、バリアント選択時に対応する画像に自動切り替えさせるには別途 JavaScript 実装が必要。
タグ:producttemplatevariantcartlegacy

コード

76 行 / liquid
<div id="product-page">
  <h2 class="heading-shaded">{{ product.title }}</h2>
  <div id="product-details">
  <div id="product-images">
    {% for image in product.images %}
      {% if forloop.first %}
        <a href="{{ image | product_img_url: 'large' }}" class="product-image" rel="lightbox[ product]" title="">
          <img src="{{ image | product_img_url: 'medium'}}" alt="{{product.title | escape }}" />
        </a>
      {% else %}
        <a href="{{ image | product_img_url: 'large' }}" class="product-image-small" rel="lightbox[ product]" title="">
          <img src="{{ image | product_img_url: 'small'}}" alt="{{product.title | escape }}" />
        </a>
      {% endif %}
    {% endfor %}
  </div>

  <ul id="product-info">
    <li>Vendor: {{ product.vendor | link_to_vendor }}</li>
    <li>Type: {{ product.type | link_to_type }}</li>
    </ul>

    <small>{{ product.price_min | money }}{% if product.price_varies %} - {{ product.price_max | money }}{% endif %}</small>

    <div id="product-options">
              {% if product.available %}

    <form action="/cart/add" method="post">

      <select id="product-select" name='id'>
        {% for variant in product.variants %}
          <option value="{{ variant.id }}">{{ variant.title }} - {{ variant.price | money }}</option>
        {% endfor %}
      </select>

      <div id="price-field"></div>

      <div class="add-to-cart"><input type="image" name="add" value="Add to Cart" id="add" src="{{ 'add-to-cart.gif' | asset_url }}" /></div>
    </form>
              {% else %}
                  <span>Sold Out!</span>
              {% endif %}
    </div>

    <div class="product-description">
    {{ product.description }}
    </div>
  </div>
</div>


<script type="text/javascript">
<!--
  // mootools callback for multi variants dropdown selector
  var selectCallback = function(variant, selector) {
    if (variant && variant.available == true) {
      // selected a valid variant
      $('add').removeClass('disabled'); // remove unavailable class from add-to-cart button
      $('add').disabled = false;           // reenable add-to-cart button
      $('price-field').innerHTML = Shopify.formatMoney(variant.price, "{{shop.money_with_currency_format}}");  // update price field
    } else {
      // variant doesn't exist
      $('add').addClass('disabled');      // set add-to-cart button to unavailable class
      $('add').disabled = true;              // disable add-to-cart button
      $('price-field').innerHTML = (variant) ? "Sold Out" : "Unavailable"; // update price-field message
    }
  };

  // initialize multi selector for product
  window.addEvent('domready', function() {
    new Shopify.OptionSelectors("product-select", { product: {{ product | json }}, onVariantSelected: selectCallback });
  });
-->
</script>

出典・ライセンス

License:
MIT

このコードは Shopify 著作の MIT ライセンスソースです。 原本の著作権は Shopify が保有します。日本語訳は ALSEL によるものです。

関連項目

📄 ページテンプレート初級

ページテンプレート

Shopify の汎用ページテンプレート。ページのタイトルと本文コンテンツを Shopify 管理画面から取得して表示する基本的な構造。

📁 liquid·MIT·5
📄 ページテンプレート初級

リダイレクト

ヘッドレステーマで Shopify ストアフロントへのリダイレクト機能を提供するセクション。管理画面に「リダイレクト」という名称で登場し、ページから別 URL への誘導を実装する基盤となる。

📁 shopify-headless-theme·MIT·7
📄 ページテンプレート初級

トップページの基本テンプレート

Shopify Liquid の最小限のテンプレート例。HTML に日付変数を埋め込み、商品ページへのリンクを配置する基本構造を示す。

📁 liquid·MIT·7
📄 ページテンプレート初級

固定ページのテンプレート

Shopify 管理画面で作成した固定ページ(利用規約、プライバシーポリシーなど)を表示するテンプレート。ページタイトルと本文コンテンツを描画する。

📁 mcliquid-theme·MIT·8
📄 ページテンプレート初級

ページの基本テンプレート

固定ページ(About など)の基本レイアウト。ページタイトルとコンテンツを HTML の article 要素でマークアップし、管理画面で編集したテキストを表示する。

📁 liquid·MIT·9
📄 ページテンプレート初級

固定ページのテンプレート

固定ページ(ポリシー、会社概要、利用規約など)の基本レイアウト。ページタイトルと本文を中央揃えで表示する。

📁 ks-bootshop·MIT·10