3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Shopify]どのページからでも特定のコレクションを抜き出す

Last updated at Posted at 2020-11-14
sample.liquid
{% assign ladies_collection = collections | where: "handle", "ladies" | first %}
{% assign tops_collection = collections | where: "title", "トップス" | first %}
<div>{{ ladies_collection.title }}</div>
<div>{{ tops_collection.title }}</div>
レディース
トップス

・collectionsオブジェクト: そのショップの全てのcollectionオブジェクトを要素として持っている配列。
・whereフィルター: 条件を指定してその条件に該当する特定のオブジェクトを抜き出し、配列として返す
・firstフィルター: 配列の最初の要素を取り出す

firstフィルターをかけるか[0]でインデックスを指定しないと、配列のままなので注意(上の場合、何も表示されない)。

参考

collectionsオブジェクト
https://shopify.dev/docs/themes/liquid/reference/objects#collections

whereフィルター
https://shopify.github.io/liquid/filters/where/

firstフィルター
https://shopify.github.io/liquid/filters/first/

追記(2020/11/30)

これでもいけました。こちらの方が簡単です。

sample.liquid
{% comment %}collections[{コレクションのhandle}]{% endcomment %}
{% assign ladies_collection = collections['ladies_collection'] %}

{% comment %}collections.{{コレクションのhandle}}{% endcomment %}
{% assign tops_collection = collections.tops %}

<div>{{ ladies_collection.title }}</div>
<div>{{ tops_collection.title }}</div>
レディース
トップス

なんでcollectionsオブジェクトのインデックスにコレクションのhandle入れると、オブジェクトが取り出せるんだろう。
どこかにドキュメントないかな。。。
→これかな。

Accessing handle attributes
In many cases you may know the handle of a object whose attributes you want to access. You can access its attributes by pluralizing the name of the object, then using either the square bracket ( [ ] ) or dot ( . ) notation.
https://shopify.dev/docs/themes/liquid/reference/basics/handle

collectionsオブジェクトって、collectionオブジェクトを含んでいるわけで、おそらくArrayとは、やっぱりちょっと違うんだなぁ(オブジェクトの配列を作るのって、できなかったはず)
collectionsとかpagesとか、Global objectsだけ特殊(Arrayだけどhandleを使ってハッシュやオブジェクトのようにアクセスできる)なのかな?
→あ、Global Objectsなんだから、オブジェクトなのか(handleがkey)。。。

参考:
You have the collection handle. collections is a global Liquid object. Therefore you can reference any specific collection using the handle.
Shopifyのcollectionに関するオブジェクト

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?