バリエーションの一覧表示を重複しないようにしたい

Topic summary

複数オプションを持つ商品の一覧でバリエーションが重複表示される問題。タグ「not to be displayed separately」が付いた商品は非表示。例:Tシャツにカラー2色・サイズ3種で、options×variantsの二重ループにより12件表示。

実装状況:main-collectionのLiquidでproduct.optionsとproduct.variantsをネストして各variantを描画。単一オプションやタグ付きは通常のproduct描画。

提案:汎用ではなく「カラー」「サイズ」の2オプションに限定し、カラーのみを一意表示。

  • 前提:カラーがvariant.option1に入る(=1番目のオプション)。
  • 仕組み:variant_colorsに表示済みカラーを記録し、同じカラーはスキップ。
  • カラー位置が不定なら、product.options内で「カラー」の位置を特定して処理を書き換える。
  • 2オプション条件を満たさない場合は従来通り全バリエーションを表示。

補足:コードスニペットが中心。技術要素はShopifyのLiquidテンプレート、product.options/variants、レンダー部分テンプレート。

進捗:回答に対し感謝の返信あり。実装確認・汎用的解決は未了で、議論は継続中。

Summarized with AI on February 15. AI used: gpt-5.

現在、バリエーション登録している商品を、商品一覧に表示されるように実装してあります。

※商品タグに「not to be displayed separately」を付けた場合は一覧表示されない。

この場合、オプションが1つのみであれば問題ないのですが、商品に複数のオプションがあると重複して表示されてしまいます。

例えば、Tシャツに「カラー(2色)」「サイズ(3サイズ)」あった場合

2×3+2×3=12 で12のバリエーションが一覧として表示されます。

そのため、オプションが複数ある商品は、1つのオプションのみ一覧表示させる、

などの条件を加えたいです。

ご教授いただけたら幸いです。

よろしくお願いいたします。

main-collectionに以下を記述

{% for product in collection.products %}
{% assign variation = false %}
{% assign variationsItem = false %}
{% if product.tags contains ‘not to be displayed separately’ %}
{% assign variationsItem = true %}
{% endif %}

{% if product.variants.size > 1 and variationsItem == false %}
{% assign variation = true %}
{% for option in product.options %}
{% for variant in product.variants %}
{% render ‘product-block-variant’,mainproduct:product, product: variant, custom_aspect_ratio: chosen_aspect_ratio, animate: true %}
{% endfor %}
{% endfor %}
{% endif %}
{% if variation == false %}
{% render ‘product-block’, product: product, custom_aspect_ratio: chosen_aspect_ratio, animate: true %}
{% endif %}
{% endfor %}

{% endif %}

汎用的な解決方法ではありませんが、

例で示されているように、

「カラー」と「サイズ」の2つのオプションがある場合にのみ回答いたします。

通常、「カラー」だけを表示されたいと思いますので、下記のようにされてはいかがでしょうか?

※動作確認はしておりません。

{% for product in collection.products %}
    {% assign variation = false %}
    {% assign variationsItem = false %}
    {% if product.tags contains 'not to be displayed separately' %}
        {% assign variationsItem = true %}
    {% endif %}

    {% if product.variants.size > 1 and variationsItem == false %}
        {% assign variation = true %}
        {% assign has_two_options = false %}
        {% if product.options contains 'カラー' and product.options contains 'サイズ' %}
            {% assign has_two_options = true %}
        {% endif %}
        {% if has_two_options %}
            {% for option in product.options %}
                {% if option.name == 'カラー' %}
                    {% assign variant_colors = '' %}
                    {% for variant in product.variants %}
                        {% unless variant_colors contains variant.option1 %}
                            {% render 'product-block-variant',mainproduct:product, product: variant, custom_aspect_ratio: chosen_aspect_ratio, animate: true %}
                            {% assign variant_colors = variant_colors | append: variant.option1 | append: ',' %}
                        {% endunless %}
                    {% endfor %}
                {% endif %}
            {% endfor %}
        {% else %}
            {% for variant in product.variants %}
                {% render 'product-block-variant',mainproduct:product, product: variant, custom_aspect_ratio: chosen_aspect_ratio, animate: true %}
            {% endfor %}
        {% endif %}
    {% endif %}
    {% if variation == false %}
        {% render 'product-block', product: product, custom_aspect_ratio: chosen_aspect_ratio, animate: true %}
    {% endif %}
{% endfor %}
  • カラーとサイズ、2つのオプションを持つ時だけ特殊処理をします。

  • optionの1つ目がカラーであることを前提としています。カラーが入る順番に応じて、

    {% unless variant_colors contains variant.option1 %}を書き換えてください。

    もし何番目にカラーが入るかを固定化できない場合は、product.optionsの何番目にカラーがあるかを数え上げて、その数値を使って、処理を書き換えてください。

ご参考まで。

(キュー田辺)

ご教示ありがとうございます!