# 商品ページで、その商品が特定のコレクションに含まれているか知りたい。

**URL:** <https://community.shopify.com/t/topic/42527>\
**Category:** Shopify テーマ\
**Created:** [April 9, 2021, 4:37am UTC](https://community.shopify.com/t/topic/42527 "2021-04-09T04:37:02Z")\
**Posts on this page:** 5\
**Page:** 1

## AI-generated summary

商品ページで、対象の商品が特定のコレクションに属しているかを判定する方法が案内された。中心となる解決策は、ShopifyのLiquidで使える `product.collections` 属性を確認すること。`product` オブジェクトに紐づくコレクション一覧をループ処理し、条件に合うものがあるかを判定する。

- 提案された方法:
  - `for` で `product.collections` を順に確認
  - `collection.handle` などの値で特定コレクションかを判別
  - 一致したらフラグ変数を `true` にする

- 共有されたコード例:
  - `isSale` を初期値 `false` に設定
  - `collection.handle == "Sale"` のとき `isSale = true`

補足として、判定には `handle` のほか `title` の利用も可能とされた。公式ドキュメントへの参照も提示されており、実装方針はほぼ固まっている。最終的な質問者からの確認返信はなく、実際に解決済みかは未確定。

<div class="post-metadata">

**Author:** ![Anonymous\_a7f2ebb168cb637970ab2dd678b2a8d1](https://avatars.discourse-cdn.com/v4/letter/a/7bcc69/32.png) [@Anonymous\_a7f2ebb168cb637970ab2dd678b2a8d1](https://community.shopify.com/u/Anonymous_a7f2ebb168cb637970ab2dd678b2a8d1)\
**Post date:** [April 9, 2021, 4:37am UTC](https://community.shopify.com/t/topic/42527/1 "2021-04-09T04:37:02Z")

</div>

いつもお世話になっております。

商品ページで、その商品が特定のコレクションに含まれているか知りたいです。  
よろしくお願い致します。

---

<div class="post-metadata">

**Author:** ![abekeishi](https://yyz1.discourse-cdn.com/shopifyforum/user_avatar/community.shopify.com/abekeishi/32/50825_2.png) [@abekeishi](https://community.shopify.com/u/abekeishi)\
**Post date:** [April 10, 2021, 2:23pm UTC](https://community.shopify.com/t/topic/42527/2 "2021-04-10T14:23:14Z")

</div>

公式のドキュメントにあるように、product オブジェクトに collections 属性があるのでそれで判定するのがいいのでではないでしょうか

[https://shopify.dev/docs/themes/liquid/reference/objects/product#product-collections](https://shopify.dev/docs/themes/liquid/reference/objects/product#product-collections)

{% for %}{% endfor %}などを用いて、collections の中から1つづつ collection の値を取り出して、特定のコレクションに含まれているか確認できるはずです。

---

<div class="post-metadata">

**Author:** ![Anonymous\_44fbe07fc59d9ccaacd70575fd5a5155](https://yyz1.discourse-cdn.com/shopifyforum/user_avatar/community.shopify.com/anonymous_44fbe07fc59d9ccaacd70575fd5a5155/32/194200_2.png) [@Anonymous\_44fbe07fc59d9ccaacd70575fd5a5155](https://community.shopify.com/u/Anonymous_44fbe07fc59d9ccaacd70575fd5a5155)\
**Post date:** [April 16, 2021, 2:36am UTC](https://community.shopify.com/t/topic/42527/3 "2021-04-16T02:36:18Z")

</div>

```ruby
{% assign isSale = false %}
{% for collection in product.collections %}
  {% if collection.handle == "Sale" }
    {% assign isSale = true %}
  {% endif %}
{% endfor %}

```

---

<div class="post-metadata">

**Author:** ![Anonymous\_44fbe07fc59d9ccaacd70575fd5a5155](https://yyz1.discourse-cdn.com/shopifyforum/user_avatar/community.shopify.com/anonymous_44fbe07fc59d9ccaacd70575fd5a5155/32/194200_2.png) [@Anonymous\_44fbe07fc59d9ccaacd70575fd5a5155](https://community.shopify.com/u/Anonymous_44fbe07fc59d9ccaacd70575fd5a5155)\
**Post date:** [April 16, 2021, 2:36am UTC](https://community.shopify.com/t/topic/42527/4 "2021-04-16T02:36:57Z")

</div>

上記コードで、Saleコレクションに属しているかを判別しております。handleという概念で判断をしているので、適宜titleなどもご利用ください。

---

<div class="post-metadata">

**Author:** ![Anonymous\_44fbe07fc59d9ccaacd70575fd5a5155](https://yyz1.discourse-cdn.com/shopifyforum/user_avatar/community.shopify.com/anonymous_44fbe07fc59d9ccaacd70575fd5a5155/32/194200_2.png) [@Anonymous\_44fbe07fc59d9ccaacd70575fd5a5155](https://community.shopify.com/u/Anonymous_44fbe07fc59d9ccaacd70575fd5a5155)\
**Post date:** [April 23, 2021, 1:45am UTC](https://community.shopify.com/t/topic/42527/5 "2021-04-23T01:45:36Z")

</div>

本件もし解決になっておりましたら、解決＆ベストアンサーの設定をお願いいたします！ 👶
