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

商品詳細ページ(レガシー実装)

商品画像のギャラリー、説明文、バリアント選択ドロップダウン、在庫状態に応じた購入ボタン制御を備えた商品詳細ページのテンプレート。MooTools ライブラリを使った JavaScript で動的なバリアント切替に対応している。

用途
Shopify の初期段階のテーマで、シンプルな商品詳細ページを構築するときに使用。バリアント選択時に価格と購入ボタンの状態をリアルタイム更新する。
設置場所
templates/product.liquid テンプレートファイルに配置する。または product セクション / スニペットとして分割実装する場合は、main-product.liquid や product-form.liquid に分けて組み込む。
注意点
このコードは MooTools フレームワーク(廃止済み)と古い Shopify.OptionSelectors API に依存しており、現在の Online Store 2.0 テーマとは互換性がない。product_img_url フィルターのサイズ指定('large'、'medium'、'small')は時代によって使用可能なサイズが変わるため、現在のストアで対応しているサイズに置き換える必要がある。asset_url フィルターで参照する 'purchase.png' は theme/assets/ ディレクトリに実際のファイルが存在しないと 404 になるため、画像ファイルを確保するか button 要素に切り替える。
タグ:productvariantformlegacyjavascriptgallery

コード

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

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

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

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

    <input type="image" src="{{ 'purchase.png' | asset_url }}" name="add" value="Purchase" id="purchase" />
  </form>
  {% else %}
    <p class="bold-red">This product is temporarily unavailable</p>
  {% endif %}

  <div id="product-details">
    <strong>Continue Shopping</strong><br />
    Browse more {{ product.type | link_to_type }} or additional {{ product.vendor | link_to_vendor }} products.
  </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
      $('purchase').removeClass('disabled'); // remove unavailable class from add-to-cart button
      $('purchase').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
      $('purchase').addClass('disabled');      // set add-to-cart button to unavailable class
      $('purchase').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