2 years ago

#61551

test-img

Emleh

Loop through only certain collections (Shopify)

I'm working on a new Shopify theme but am getting stuck on one thing. I want to create a shop page that

  • Displays collections, but not all of them: I want to show only the collections that a) contain products, OR b) that have a certain metafield set to 'true' (= a metafield indicating it is a 'fixed collection' i.e. should be displayed regardless of whether the products in it are out of stock)
  • Paginates these collections by 20 (because there are a lot of collections in the store - paginating them decreases load time)

This is what I've got as Liquid code (simplified for explanation purposes):

{% paginate collections by 20 %}

{% for collection in collections %}

{% assign fixedCollection = collection.metafields.collections.fixedCollection %}

{% if collection.all_products_count > 0 or fixedCollection == true %}
<div class="collection">
// collection contents (title, image etc.)
</div>
{% endif %}

{% endfor %}

{% if paginate.pages > 1 %}
{% include 'pagination-custom' %}
{% endif %}

{% endpaginate %}

The issue I'm having is that each page doesn't always contain 20 collections - it's usually less. Because it loops through 20 collections each time but only displays the ones meeting the conditions set by the 'if' statement.

Is there a way to limit the for-loop in some way, so it only loops through certain collections? Or does someone have a better solution for this?

Grateful for any help on this!

shopify

liquid

shopify-template

0 Answers

Your Answer

Accepted video resources