# メタフィールドで選択したコレクションのリンクを商品ページに追加したい

**URL:** <https://community.shopify.com/t/topic/379904>\
**Category:** 技術的なQ＆A\
**Created:** [December 10, 2024, 8:03am UTC](https://community.shopify.com/t/topic/379904 "2024-12-10T08:03:40Z")\
**Posts on this page:** 6\
**Page:** 1

## AI-generated summary

商品ページにメタフィールドで選択したコレクションのリンクとテキストを表示する方法についての質問と解決。

**問題点:**
- メタフィールドでコレクションを選択し、Liquidコードで表示しようとしたが、コレクション名が表示されず「CollectionListDrop」と表示される
- メタフィールドから取得される値がGID形式(例: `gid://shopify/Collection/コレクションID`)になっており、直接`collections[handle]`に渡せない

**解決プロセス:**
- まず基本的なコレクションの表示確認を実施
- GIDから実際のハンドルを取得する必要があることが判明
- GIDを「/」で分割してコレクションIDを抽出し、`where`フィルターでハンドルを取得する方法で解決

**最終的な解決策:**
GIDを分割してIDを取得 → `collections`から該当コレクションのハンドルを検索 → そのハンドルを使ってコレクション情報を表示

参考リンク: Shopify Communityの類似質問スレッド

**ステータス:** 解決済み

<div class="post-metadata">

**Author:** ![staff\_m](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@staff\_m](https://community.shopify.com/u/staff_m)\
**Post date:** [December 10, 2024, 8:03am UTC](https://community.shopify.com/t/topic/379904/1 "2024-12-10T08:03:40Z")

</div>

初心者です。

メタフィールドは各カテゴリごとのカスタムデータ定義、メタオブジェクトはショップ全体のカスタムデータ定義と理解しています。

Liquidもかじった程度です。

商品ページにコレクションで作成したページのテキストとリンクを記載したいと考えています。

メタフィールドにて下記の通りコレクションのフィールドを定義しました。

 ![staff_m_0-1733817188502.png](https://canada1.discourse-cdn.com/shopifyforum/original/3X/1/e/1e9e8bb78ff501dc36b23fb1cbe5f7636b1898a3.png)

商品ページで該当のコレクションを選び、

テーマの設定で「カスタムLiquid」ブロックに下記コードを入力しましたが、プレビューでは「CollectionList Drop」と表示され、コレクション名が表示されません。

```auto
{% if product.metafields.custom.brands.value != blank %}
<div>
{{product.metafields.custom.brands.value}}
</div>
{% endif %}

```

![staff_m_1-1733817533827.png](https://canada1.discourse-cdn.com/shopifyforum/original/3X/1/2/129ac63bc18cba642a3f699ceb7e282dd04095b4.png)

コレクション名とコレクションのリンクを記載するにはどうしたら良いのでしょうか。

---

<div class="post-metadata">

**Author:** ![ROUTE06\_baba](https://yyz1.discourse-cdn.com/shopifyforum/user_avatar/community.shopify.com/route06_baba/32/106703_2.png) [@ROUTE06\_baba](https://community.shopify.com/u/ROUTE06_baba)\
**Post date:** [December 10, 2024, 8:24am UTC](https://community.shopify.com/t/topic/379904/2 "2024-12-10T08:24:28Z")

</div>

以下でどうでしょう？

```markup
{% if product.metafields.custom.brands %}
  {% assign collection_handle = product.metafields.custom.brands %}
  {% assign brands = collections[collection_handle] %}
  
  {% if brands %}
    {{ brands.title }}
  {% else %}
    

関連するコレクションは見つかりませんでした。

  {% endif %}
{% else %}
  

関連するコレクションは設定されていません。

{% endif %}

```

---

<div class="post-metadata">

**Author:** ![staff\_m](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@staff\_m](https://community.shopify.com/u/staff_m)\
**Post date:** [December 10, 2024, 9:35am UTC](https://community.shopify.com/t/topic/379904/3 "2024-12-10T09:35:38Z")

</div>

早急なご回答ありがとうございます！

ご教示いただいた内容で修正したのですが、コレクションのリンクは表示されませんでした。

if分の条件には合致しているようで、試しに下記のように

aaa

を追加したらプレビュー画面に「aaa」は表示されました。

```markup
{% if product.metafields.custom.brands %}
  {% assign collection_handle = product.metafields.custom.brands %}
  {% assign brands = collections[collection_handle] %}
  
  {% if brands %}
    

aaa

    {{ brands.title }}
  {% else %}
    

関連するコレクションは見つかりませんでした。

  {% endif %}
{% else %}
  

関連するコレクションは設定されていません。

{% endif %}

```

{{collection\_handle}}には[“gid://shopify/Collection/コレクションID”]が入っているようなのですが、こちらがbrandsに代入する際にうまくいっていないのでしょうか？

---

<div class="post-metadata">

**Author:** ![ROUTE06\_baba](https://yyz1.discourse-cdn.com/shopifyforum/user_avatar/community.shopify.com/route06_baba/32/106703_2.png) [@ROUTE06\_baba](https://community.shopify.com/u/ROUTE06_baba)\
**Post date:** [December 10, 2024, 10:33am UTC](https://community.shopify.com/t/topic/379904/4 "2024-12-10T10:33:38Z")

</div>

ご返信ありがとうございます。

であれば、ひとつひとつ確認しながら進めて、最終的に別の形式のハンドルをcollectionsに渡して値を引っ張ってくるといいかもしれません。

まず以下のコードで、ストアのすべてのコレクションのハンドルとタイトルリンクを表示することができるはずです（ここがまずうまくいかなかったらご指摘ください）

```markup
{% for collection in collections %}
  - {{ collection.handle }}：{{- collection.title | link_to: collection.url }}

{% endfor %}

```

すると、表示したいコレクションのハンドルが何かがわかるはずなので、

```markup
{% assign brands = collections['ハンドル名'] %}

```

と、直接ハンドル名の文字列を渡してあげてください。うまくいけば、brandsに正しいコレクションが設定されると思います。

参考までに、collectionsについての公式ドキュメントはこちらです。

[https://shopify.dev/docs/api/liquid/objects/collections#collections-access-a-specific-collection](https://shopify.dev/docs/api/liquid/objects/collections#collections-access-a-specific-collection)

---

<div class="post-metadata">

**Author:** ![staff\_m](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@staff\_m](https://community.shopify.com/u/staff_m)\
**Post date:** [December 11, 2024, 3:51am UTC](https://community.shopify.com/t/topic/379904/5 "2024-12-11T03:51:16Z")

</div>

ご返信ありがとうございます！

回答いただいた確認項目はどちらも問題なくできました。

メタフィールドで取得する情報がgidになってしまうようなので、下記を参考にidからhandleを取得する方法にしてやりたいことが実現できました！

How to get product Information by Product ID ?  
[https://community.shopify.com/topic/1604231](https://community.shopify.com/topic/1604231)

ご参考までにコード貼っておきます。

```markup
{% if product.metafields.custom.brands %}
  {% assign split_id = product.metafields.custom.brands | split:"/" %}
  {% assign the_id = split_id |last %}
  {% assign collection_id = the_id | times:1 %}
  {% assign collection_handle = collections | where: 'id', collection_id | first %}
  {% assign brands = collections[collection_handle.handle] %}
  
  {% if brands %}
    {{ brands.title }}
  {% else %}
    

関連するコレクションは見つかりませんでした。

  {% endif %}
{% else %}
  

関連するコレクションは設定されていません。

{% endif %}

```

この度は早急なご回答大変助かりました。

---

<div class="post-metadata">

**Author:** ![ROUTE06\_baba](https://yyz1.discourse-cdn.com/shopifyforum/user_avatar/community.shopify.com/route06_baba/32/106703_2.png) [@ROUTE06\_baba](https://community.shopify.com/u/ROUTE06_baba)\
**Post date:** [December 11, 2024, 3:52am UTC](https://community.shopify.com/t/topic/379904/6 "2024-12-11T03:52:06Z")

</div>

解決してよかったです！ 🎉
