Filter only available products while using pagination

Filter only available products while using pagination

hardow2011
Tourist
8 0 2

Hello. I had a problem that a lot of people seemed to had, and, as I wasn't able to find the answer I fixed it myself and I am sharing the answer.

I had pagination enabled, plus I wanted to hide the unavailable products. It is done like so.

In the collection-template.liquid, change:

{% paginate collection.products by 20 %}

For

{% paginate collection.products | where: "available", true by 20 %}

Then, locate the loop:

{% for product in collection.products %}
    {% assign featured = product %}
    <div class="grid__item {{grid_item_width}}">
    {% include 'product-grid-item' %}
    </div>
...

And replace it for:

{% for product in collection.products %}
    {% if product.available == true %}
        {% assign featured = product %}
        <div class="grid__item {{grid_item_width}}">
        {% include 'product-grid-item' %}
        </div>
    {% endif %}

...

Replies 3 (3)

markmirasol
Tourist
6 2 0

Thank you for posting this, sir. Unfortunately it did not work for me.

https://community.shopify.com/c/Shopify-Design/Filter-only-available-products-while-using-pagination...

Tiber
Shopify Partner
31 4 6

This does not work for me

 

{% paginate blogs['recipe-database'].articles | where 'tags','Chocolate'  by 50  %}
     
      	{% for article in blog.articles %}
     
           <h2>{{article.title}}</h2>
     
        {% endfor %}
      {% endpaginate %}
CalebV
Shopify Partner
15 0 5

What behavior did you observe from this? Did the pagination display the normal number of pages and hide the products that did not meet the criteria?