Liquid Snippets by ALSEL
🏗️ テーマ基盤中級

国・通貨選択モーダル

ストアの利用可能国から顧客が自分の国と通貨を選択するモーダルウィンドウ。Bootstrap の modal コンポーネントで実装し、Shopify の localization オブジェクトから国リストと現在の選択状態を取得する。

用途
ヘッダーのグローバルナビに「国・通貨切替」ボタンを配置し、クリック時にこのモーダルを開いて複数国対応ストアの顧客が配送先国と表示通貨を切り替えるシーン。
設置場所
snippets/modal-localization.liquid に配置し、theme.liquid またはヘッダー section 内で `{% render 'modal-localization' %}` で呼び出す。モーダルを開くボタンには `data-bs-toggle="modal" data-bs-target="#modal-localization"` 属性を付ける。
注意点
localization.available_countries が Shopify 管理画面で設定されていることが前提。country | image_url で国旗画像を取得するため、テーマに Shopify が提供する国旗 SVG が含まれていることを確認する必要がある。Bootstrap 5 以上の CSS と JavaScript(特に modal 機能)がテーマに読み込まれている必須。form 'localization' は Shopify の自動送信フォームなので、送信直後にページ遷移またはリロードが発生する。
タグ:localizationmodalcountrycurrencybootstrap

コード

47 行 / liquid
{% if localization.available_countries.size > 1 %}
  <div 
    id="modal-localization" 
    class="modal fade" 
    tabindex="-1" 
    aria-labelledby="modal-localization-label"
    aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h2 id="modal-localization-label" class="modal-title">
            {{ 'general.localization.title' | t }}
          </h2>
          <button class="btn-close" type="button" data-bs-dismiss="modal" aria-label="{{ 'general.accessibility.close' | t }}">
            {% render 'svg-icons', icon: 'x', size: 20 %}
          </button>
        </div>
        <div class="modal-body">
          {% form 'localization' %}
            <input type="hidden" name="country_code" value="{{ localization.country.iso_code }}">
            <ul class="list-group list-group-flush" aria-labelledby="modal-localization-label">
              {% for country in localization.available_countries %}
                <button
                  class="list-group-item list-group-item-action {% if country.iso_code == localization.country.iso_code %}active{% endif %}"
                  data-iso-code="{{ country.iso_code }}"
                  aria-current="{% if country.iso_code == localization.country.iso_code %}true{% endif %}"
                  role="button">
                  <div class="localization-form-item-img me-4" aria-hidden="true">
                    {{ country | image_url: width: 20, height: 20 | image_tag }}
                  </div>
                  <div class="localization-form-item-text">
                    <span>
                      {{ country.name }}
                    </span>
                    <small class="opacity-50">
                      {{ country.currency.iso_code }} {{ country.currency.symbol }}
                    </small>
                  </div>
                </button>
              {% endfor %}
            </ul>
          {% endform %}
        </div>
      </div>
    </div>
  </div>
{% endif %}

出典・ライセンス

License:
MIT

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

関連項目

🏗️ テーマ基盤中級

セクションスキーマのカスタムブロック定義

セクションの schema ブロック内でカスタムブロックタイプを定義する方法を示すサンプル。@theme と _private というプレフィックスで異なるブロック種別を宣言する。

📁 theme-tools·MIT·7
🏗️ テーマ基盤初級

セクション呼び出しのフォーマット

Liquid の section タグを複数行に記述する際の正しいフォーマット例。タグ名を改行で分割しても正しく認識される書き方を示す。

📁 theme-tools·MIT·7
🏗️ テーマ基盤初級

レイアウトテンプレートの指定

Liquid の `layout` タグを使い、テーマのレイアウトファイルを指定する構文。複数のレイアウト候補と whitespace 制御(ダッシュ記号)の使い方を示す。

📁 theme-tools·MIT·8
🏗️ テーマ基盤上級

Liquid フォーマッター互換のセクション表記

Prettier Liquid プラグインのテストケースで、セクションタグの様々な書式を検証する。異なる間隔・改行・ホワイトスペーストリミングの組み合わせでもセクション名が崩れないことを確認する。

📁 theme-tools·MIT·9
🏗️ テーマ基盤初級

基本的なHTMLレイアウト

Prettier Liquid プラグインのテスト用に作られた最小限の HTML テンプレート。DOCTYPE から body タグまでの基本的なドキュメント構造を示す。

📁 theme-tools·MIT·10
🏗️ テーマ基盤上級

layout タグのフォーマット

Liquid の layout タグの書式をコード品質ツール(Prettier)でフォーマットするテストケース。スペースや改行位置の違いを正規化し、レイアウト名が壊れないことを検証する。

📁 theme-tools·MIT·10