Liquid、JavaScriptなどに関する質問
現在、バリエーション登録している商品を、商品一覧に表示されるように実装してあります。
※商品タグに「not to be displayed separately」を付けた場合は一覧表示されない。
この場合、オプションが1つのみであれば問題ないのですが、商品に複数のオプションがあると重複して表示されてしまいます。
例えば、Tシャツに「カラー(2色)」「サイズ(3サイズ)」あった場合
2×3+2×3=12 で12のバリエーションが一覧として表示されます。
そのため、オプションが複数ある商品は、1つのオプションのみ一覧表示させる、
などの条件を加えたいです。
{% 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 %}
</div>
{% 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つのオプションがある場合にのみ回答いたします。
通常、「カラー」だけを表示されたいと思いますので、下記のようにされてはいかがでしょうか?
※動作確認はしておりません。
{% 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 %}
ご参考まで。
(キュー田辺)
Shopify アカデミーの学習パスと認定スキルバッジExpanding Your Shopify Business Internationallyを活用して、国際的にビジネ...
By Shopify Feb 7, 2025Shopify アカデミーの学習パスB2B on Shopify:立ち上げとカスタマイズで卸売販売に進出しましょう。これら3つの無料コースは、ShopifyストアでB2B機能...
By Shopify Jan 31, 2025サポートの選択肢が増えていく中、最適となる選択の判断が難しくなっているかと存じます。今回は問題の解決に最適となるサポートの選択方法を、紹介させて頂きます。 選択肢のご紹介...
By Mirai Oct 6, 2024