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

カートページテンプレート

カートの商品一覧を表示し、数量変更・商品削除・チェックアウトに対応するカートページ。商品画像、バリアント情報、金額をカード形式で表示し、レスポンシブ対応で PC とモバイルの両環境で動作する。

用途
ストアのカートページ(/cart)の主要テンプレート。顧客が商品を確認し、数量調整してチェックアウトに進むまでの一連の流れを実装する。
設置場所
templates/cart.liquid に配置する。ストアのテーマファイル構成に応じて、必要に応じて section や snippet に分割してから各ページテンプレートで呼び出す構成も可。
注意点
数量入力フィールドは name="updates[]" で POST 送信され、form action="/cart" で cart.json に反映される仕様になっているため、入力値は数値として扱い非表示の検証を Shopify 側で行う前提。商品削除は JavaScript の remove_item() 関数で input value を 0 にして submit するため、JavaScript が無効な環境では削除機能が動かないので注意。content_for_additional_checkout_buttons は Shopify が自動挿入する追加決済ボタン(Apple Pay、Google Pay など)のプレースホルダーなので削除・上書きしない。
タグ:carttemplatequantitycheckoutresponsive

コード

177 行 / liquid
<script>
  <!--
  function remove_item(id)
  {
      document.getElementById('updates_'+id).value = 0;
      document.getElementById('cart').submit();
  }
  -->
</script>

<div class="cart-container">
  <h1 class="section-title">Order</h1>
  <div class="cart-content">
    {% if cart.item_count == 0 %}
      <div class="alertbox">
        <div class="alertcontent">
          <strong>Your shopping cart is empty</strong>

          <p>Note that this can happen if you disabled cookies in your browser</p>
          <p>We strongly recommends to enable cookies. Many webpages need them to work properly.</p>

          <p>{{ 'Back to frontpage' | link_to: '/' }}</p>
        </div>
      </div>

    {% else %}
      <form class="cart-form" action="/cart" id="cart" method="post">
        <div id="shoppingcart">
          {% for item in cart.items %}
            <div class="itemrow">
              <div class="itemrow-product">
                {{
                  item.product.featured_image
                  | asset_img_url: 'pico'
                  | img_tag: item.product.title, 'cart-image', '100x100'
                }}
                <div>
                  <h2 class="itemrow-product-title">{{ item.product.title }}</h2>
                  <p>
                    {{ item.variant.title }}
                  </p>
                  <p>
                    {{ item.variant.sku }}
                  </p>
                </div>
              </div>
              <div>
                <input
                  type="text"
                  size="4"
                  name="updates[]"
                  id="updates_{{item.id}}"
                  value="{{item.quantity}}"
                  onfocus="this.select();"
                >
              </div>
              <div>{{ item.price | money }}</div>
              <div align="right">{{ item.final_line_price | money }}</div>
              <div><a href="#" onclick="remove_item({{item.id}});" class="remove">remove</a></div>
            </div>
          {% endfor %}
          <input type="image" src="{{'b_update.gif'| asset_url}}" value="Update Cart">
        </div>

        <div class="checkout-buttons">
          <div>
            Merchandise {{ cart.total_price | money }}
            {{ shop.currency }}
          </div>
          {{ content_for_additional_checkout_buttons }}
        </div>
      </form>
    {% endif %}

    <ul>
      <li><strong>This store is only for demonstration purposes. No items will be shipped</strong></li>
    </ul>
  </div>
</div>

{% style %}
  .cart-container {
    padding: 0 1rem;
  }
  .cart-content {
    padding-top: 2rem;
  }
  .cart-image {
    height: 100px;
    width: 100px;
  }
  .section-title {
    font-size: 1rem;
    font-weight: normal;
    margin: 0;
    position: sticky;
    top: 0;
    background-color: white;
    border-bottom: 1px solid black;
    padding: 0.5rem;
  }
  .cart-form {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    gap: 1rem;
  }
  .itemrow {
    display: flex;
    flex-direction: row;
    gap: 1rem;
    align-items: center;
  }

  .itemrow-product {
    display: flex;
    flex-direction: row;
    gap: 1rem;
    align-items: center;
    flex-shrink: 0;
    flex-grow: 0;
  }
  .itemrow-product p {
    margin: 0;
    color: grey;
    font-size: 0.8rem;
  }
  .itemrow-product-title {
    font-size: 1rem;
    margin: 0;
  }
  #shoppingcart {
    flex: 1;
  }
  .checkout-buttons {
    flex-shrink: 0;
    border-left: 1px solid lightgrey;
    padding-left: 1rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
    align-items: flex-start;
    justify-content: flex-start;
  }
  .checkout-buttons input {
    background-color: #363;
    color: white;
    padding: 0.5rem 1rem;
    border: none;
    font-weight: 600;
    font-size: 0.8rem;
  }

  @media (max-width: 768px) {
    .itemrow {
      flex-direction: column;
      align-items: flex-start;
    }

    #shoppingcart {
      display: flex;
      flex-direction: column;
      gap: 1rem;
      align-items: flex-start;
    }

    .cart-form {
      flex-direction: column;
    }

    .checkout-buttons {
      border-left: none;
      padding-left: 0;
    }
  }
{% endstyle %}

出典・ライセンス

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