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

商品詳細ページテンプレート

商品のタイトル、説明、画像、バリアントセレクタ、カート追加ボタンを表示する商品詳細ページのテンプレート。バリアント選択時に在庫状態と価格を動的に更新する機能を持つ。

用途
Shopify の商品詳細ページで、商品情報の基本レイアウトと購入フロー(バリアント選択・カート追加)を実装するときに使う。
設置場所
templates/product.liquid として配置する。バリアント選択時の動的更新は `Shopify.OptionSelectors` で制御され、対応する JavaScript ライブラリが必要。
注意点
コードで使われている `Shopify.OptionSelectors` と Prototype.js は非常に古いライブラリであり、現在の Shopify テーマ(Online Store 2.0)では推奨されていない。バリアント管理は theme settings の JSON または Liquid の標準ループ構文に置き換え、JavaScript は vanilla JS または Stimulus で書き直すことを強く推奨する。product_img_url フィルタの 'medium' / 'large' / 'small' サイズは廃止予定なため、幅指定(例 `image | image_url: width=500`)に変更する。
タグ:producttemplatevariantcartlegacy

コード

117 行 / liquid
<div id="page" class="innerpage clearfix">
  <h1>{{ collection.title }} {{ product.title }}</h1>


  <p class="latest-news"><strong>Product Tags: </strong>
    {% for tag in product.tags %}
            <a href="/collections/all/{{ tag }}">{{ tag }}</a> |
          {% endfor %}
  </p>

  <div class="product clearfix">
    <div class="product-info">
      <h1>{{ product.title }}</h1>
      <div class="product-info-description">
        <p>{{ product.description }}  </p>
      </div>

      {% if product.available %}
      <form action="/cart/add" method="post">

      <h2>Product Options:</h2>

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

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

      <div class="product-purchase-btn">
        <input type="submit" class="add-this-to-cart" id="add-this-to-cart" value="Add to Basket" />
      </div>

      </form>
      {% else %}
              <h2>Sold out!</h2>
              <p>Sorry, we're all out of this product. Check back often and order when it returns</p>
              {% endif %}
    </div>

    <div class="product-images clearfix">
      {% for image in product.images %}

      {% if forloop.first %}
      <div class="product-image-large">
        <img src="{{ image | product_img_url: 'medium'}}" alt="{{product.title | escape }}" />
      </div>
      {% else %}
      {% endif %}
      {% endfor %}

      <ul class="product-thumbs clearfix">
      {% for image in product.images %}
      {% if forloop.first %}
      {% else %}

      <li>
      <a href="{{ image | product_img_url: 'large' }}" class="product-thumbs" rel="lightbox[product]" title="">
        <img src="{{ image | product_img_url: 'small'}}" alt="{{product.title | escape }}" />
      </a>
      </li>
      {% endif %}
      {% endfor %}
      </ul>
    </div>
  </div>



  <div id="three-reasons" class="clearfix">
    <h3>Why Shop With Us?</h3>
    <ul>
      <li class="two-a">
        <h4>24 Hours</h4>
        <p>We're always here to help.</p>
      </li>
      <li class="two-c">
        <h4>No Spam</h4>
        <p>We'll never share your info.</p>
      </li>
      <li class="two-d">
        <h4>Secure Servers</h4>
        <p>Checkout is 256bit encrypted.</p>
      </li>
    </ul>
  </div>

</div>
<!-- end page -->

<script type="text/javascript">
<!--
  // prototype callback for multi variants dropdown selector
  var selectCallback = function(variant, selector) {
    if (variant && variant.available == true) {
      // selected a valid variant
      $('add-this-to-cart').removeClassName('disabled'); // remove unavailable class from add-to-cart button
      $('add-this-to-cart').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-this-to-cart').addClassName('disabled');      // set add-to-cart button to unavailable class
      $('add-this-to-cart').disabled = true;              // disable add-to-cart button
      $('price-field').innerHTML = (variant) ? "Sold Out" : "Unavailable"; // update price-field message
    }
  };

  // initialize multi selector for product
  Event.observe(document, 'dom:loaded', function() {
    new Shopify.OptionSelectors("product-info-options", { 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