Shopify themes, liquid, logos, and UX
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
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!
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.
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.
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.
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.
The year-end shopping spree is around the corner! Is your online store ready for the ...
By JasonH Nov 10, 2024We recently spoke with Zopi developers @Zopi about how dropshipping businesses can enha...
By JasonH Oct 23, 2024A big shout out to all of the merchants who participated in our AMA with 2H Media: Holi...
By Jacqui Oct 21, 2024