FROM CACHE - jp_header
解決済

特定のコレクションテンプレートを使用しているコレクションのみ一覧表示したい

diamondo
観光客
5 0 1

■前提・実現したいこと


TOPページやコレクションリストページで、特定のコレクションテンプレートを使用しているコレクションのみ一覧表示したい

 

例)
コレクションA

(使用テンプレート:collection.liquid → デフォルトのコレクションテンプレ)
コレクションB

(使用テンプレート:collection.test.liquid → 独自で作成したコレクションテンプレ)
コレクションC

(使用テンプレート:collection.test.liquid)


→ B・Cのコレクションのみ表示したい

 

■この機能を実装しているshopifyサイト

 

https://store.rili.tokyo/collections
コレクションリストページに「コーデから探す」系のコレクション一覧のみ表示。
(その他、トップスやアウターなどのコレクションは表示させていない)

 

■試したこと

 

{% for collection in collections %}
{{ collection.title }}
{% endfor %}

 

このコレクション一覧表示のコードをベースに、
「特定のコレクションテンプレートを使用している」という条件を追加するために色々試したのですがうまくいきませんでした。

1 件の受理された解決策

junichiokamura
Community Manager
1200 280 506

成功

diamondoさん、こんにちは。

 

以下ののcollection idでフィルタするか、

https://help.shopify.com/en/themes/liquid/objects/collection

 

使っているテンプレートで判別する場合は以下のtemplate-suffixで判断するのはいかがでしょうか?

https://help.shopify.com/en/themes/liquid/objects/collection#collection-template_suffix

Senior Partner Solutions Engineer

元の投稿で解決策を見る

7件の返信7

junichiokamura
Community Manager
1200 280 506

成功

diamondoさん、こんにちは。

 

以下ののcollection idでフィルタするか、

https://help.shopify.com/en/themes/liquid/objects/collection

 

使っているテンプレートで判別する場合は以下のtemplate-suffixで判断するのはいかがでしょうか?

https://help.shopify.com/en/themes/liquid/objects/collection#collection-template_suffix

Senior Partner Solutions Engineer
diamondo
観光客
5 0 1

ありがとうございます!

ご教示いただきました「template-suffix」で下記コードを試したところ、コレクションテンプレートを条件にループを回すことができました。

 

 

{% for collection in collections %}
 {% if collection.template_suffix == 'coordinate' %}
  ここに処理内容
 {% endif %}
{% endfor %}

 

 

また、立て続けのご質問で大変恐縮なのですが、ページネーションの該当条件にもコレクションテンプレートを適用することは可能でしょうか?

下記のようなコードですと特定のコレクションテンプレート以外のコレクションもページネーションの数に含まれてしまいます。

 

 

{% paginate collections by 10 %}

{% for collection in collections %}
 {% if collection.template_suffix == 'coordinate' %}
  ここに処理内容
 {% endif %}
{% endfor %}

{% if paginate.pages > 1 %}
  {{ paginate | default_pagination }}
{% endif %}

{% endpaginate %}

 

 

junichiokamura
Community Manager
1200 280 506

検証はしていませんが、Filter whereを paginate collections | where:  "template_suffix", "coordinate"  のように使えませんでしょうか?

https://help.shopify.com/en/themes/liquid/filters/array-filters#where

Senior Partner Solutions Engineer
diamondo
観光客
5 0 1

 

Filter whereを使って下記のように色々試したのですが、動作しませんでした。

 

{% paginate collections | where:  "template_suffix", "coordinate" %}
{% paginate collections | where:  "template_suffix", "coordinate" by 10 %}
{% assign aaaa = paginate collections | where: "template_suffix", "coordinate"  %} 
{% paginate aaaa by 10 %}
⇒エラー表示

 

junichiokamura
Community Manager
1200 280 506

検証していただきありがとうございます。

英語ページでも調べてみましたが、現状liquidによる対応はできないようです。

https://community.shopify.com/c/Shopify-Design/Paginate-only-available-products/td-p/25762

 

AjaxによるJavaScript APIで取得するなど他の方法が必要になりそうです。

Senior Partner Solutions Engineer
diamondo
観光客
5 0 1

liquidによる対応はできないのですね。

色々とご教示いただきまして、ありがとうございます!

「特定のコレクションテンプレートを使用しているコレクションのみ一覧表示したい」という機能が実装でき、非常に助かりました。

WEBUILD
Shopify Partner
39 11 41

一度 assign でオブジェクトを定義して それから pagination に渡してはどうでしょうか?

{% assign mod_collection = collections | where:  "template_suffix", "coordinate" %}
{% pagination mod_collection by 10 %}

diamondo さんがお試しになられた3番目に近いと思いますが、

paginationを 含めた assignはできないのでエラーになっているのだと思います。
試してないので推測ですが。。。