Re: Buggy collection pagination when used within a theme section block

Buggy collection pagination when used within a theme section block

Cavera
Shopify Partner
24 0 10

I've created a new section block within my theme. That block allows you to select a collection and then, the number of products to display. I'm using paginate to control this but am getting some strange results. Here is my sample code

{% liquid
	paginate block.settings.collection.products by block.settings.num_products
		echo block.settings.collection.products.size
	endpaginate
%}

When I load the page, it will show the correct number of products but when I reload again, the number of products is 50. Reload again and it's correct again. So, basically it works correctly on every other page load, otherwise, it just returns 50.

Anyone have any thoughts? I'm totally stumped

Replies 5 (5)

Litos
Globetrotter
688 169 149

Hi @Cavera,

If you want to get the number of products of the collection, you can use the following code, it will work fine:

{{ block.settings.collection.all_products_count }}

Refer https://shopify.dev/docs/api/liquid/objects/collection#collection-all_products_count

Hope it helps!

Litos - Shopify Development Service Provider
Need to develop or customize your Shopify store, feel free to contact us here.
Cavera
Shopify Partner
24 0 10

I appreciate the response but it doesn't really address my issue. It's not that I am unable to get the number of products in a collection, it's that when I paginate a collection, the products that it is returning is not accurate or consistent. The only reason I added the following code ...

echo block.settings.collection.products.size

...is to display the number of products it is returning. That's how I know that sometimes returns the paginated amount and sometimes returns 50 products. That's the issue I'm trying to figure out.

Emby
Shopify Partner
1 0 2

I'm not sure if you found an answer elsewhere so I'll post here for anyone who might be coming across this issue.

 

Although I can't provide the exact reason why the pagination count is alternating on page load, it appears to happen any time a block or section uses the "collection" input setting. From what I've found, fetching the collection by its handle from the input setting value seems to be a stable enough workaround, however it's not exactly how I expect it was intended to work.

 

Here's an example that works for me so far:

{% liquid
	assign collection = collections[block.settings.collection.handle]
	paginate collection.products by block.settings.num_products
		echo collection.products.size
	endpaginate
%}

 

Of course you can also use section.settings in place of block.settings depending on your section schema.

tobebuilds
Shopify Partner
469 34 125

Thank you so much for this... It fixed the issue for me.

 

I have an app block where users can provide a custom pagination setting, but it was breaking/not working due to this issue.

 

It's not clear how pagination works behind the scenes, or why only some objects can use it.

 

Hopefully the Shopify team will add more documentation for this in the future.

Founder, Regios Discounts app (4.8 stars, 64 reviews, Built for Shopify)
- Custom discounts made simple
- "Just about any discount you'll ever need"
- Built by an ex-Google software engineer
tobebuilds
Shopify Partner
469 34 125

Something makes me think that the type of:

 

`block.settings.collection` or `section.settings.collection` is actually of type `string`, not `collection`

 

Whereas `collections[index]` is of type `collection`

So there is no `.products` to paginate on a string.

 

Founder, Regios Discounts app (4.8 stars, 64 reviews, Built for Shopify)
- Custom discounts made simple
- "Just about any discount you'll ever need"
- Built by an ex-Google software engineer