[Shopify] all_productsの商品取得上限は1ページ20商品の制限があるので解決してみる

[Shopify] all_productsの商品取得上限は1ページ20商品の制限があるので解決してみる

I was using "all_products" to retrieve product information in a page, but I ran into a problem.

If I use all_products more than 20 times in a page, the 21st all_products will not get the product. I've been doing a lot of research on the English base. The maximum number of products that can be retrieved by "all_products" in one page is 20. This was the case.

All_products was very convenient and useful for obtaining product information, but there is nothing we can do about not being able to use it. We'll find another way to fix this!

What is "all_products" available in Shopify?

What is Shopify's all_products, anyway?

It is very useful, for example, when you want to retrieve product information and display the product title in a fixed page. Usually, we think of it as a loop that goes through each product in the all products collection (all) and pulls out the products that correspond to a specific sku, handle, etc.

{% for product in collections.all.products %}
{% if product.handle == "product-url" %}
<p>{{ product.title }}</p>
{% endif
%}{% endfor %}

However, with all_products, this process is

{% all_products["product-url"].title %}

and it ends up on one line!

For more detailed usage, including combining with schema tags, please see the following blog post.


Maximum limit of 20 products per page for all_products

How convenient. I used to be thankful every time I used it, but then I discovered that all_products can only retrieve 20 products in one page.

Then, for example

  • If you don't want to use collections, but want to create a fixed page and arrange about 50 products
  • If you create content on a fixed page and want to tie a total of 21 or more products to each piece of content
  • I want to introduce products such as "50 recommended products" in a blog post.

In such cases, it is practically impossible to use all_products.

For stores with a small number of products, this may not be a concern, but for large stores with more than 500 or 1000 products, for example, there are quite a few situations where you may want to get more than 20 products.

How to solve the problem of getting more than 21 products on a page

Now, we will explain how to solve the problem of getting more than 21 products on a single page.

Simply put, use the method of selecting the appropriate product from the collection described in the first "What is all_products" section.

{% for product in collections.all.products %}
{% if product.handle == "product-url" %}
<p>{{ product.title }}</p>
{% endif
%}{% endfor %}

However, there are two drawbacks to this method as well.

  • The for loop of collection can only loop over 50 products
    → If the store has more than 50 products to begin with, all products cannot be acquired
  • If you want to retrieve 50 products, you have to repeat the all products collection loop 50 times
    -> Total number of products in the store x 50 times, which slows down the page display speed

And here is how to compensate for that shortcoming.

paginate collections.all.products by 1500 %}
{% for product in collections.all.products %}
{% if product.handle == "product-url1" %}
{% assign product1 = product %}

endif %}
{% if product.handle == "product-url2" %}
{% assign product2 = product %}
{% endif %}
{% endfor %}
{% endpaginate

%}<p>{{ product1.title }}</p>
<p>{{ product2.title }}</p

What we are doing is first using pagination to eliminate the upper limit of 50 products in the collection loop. In this case, the maximum number of products is set to 1500, but if your store has more than that, change it.

Then, when the product handles are "product-url1" and "product-url2", they are assigned to the variables product1 and product2, respectively.

Finally, the product titles of product1 and product2 stored in the variables are displayed.

This method can finish the all-product collection loop in one step, so I have the impression that it minimizes the delay in page display speed.

[Shopify] Resolving the all_products product acquisition limit of 20 products per page Conclusion

This is how to break through the 20-product limit for ALL_PRODUCTS.

I put this article together because I thought it might be in demand for a fairly large store. My impression is that the all_products limit of 20 products should be eliminated. I really wish they would do away with the 20-product limit for all_products.

Shopify itself is highly functional, easy to use, and very useful.

Thank you for reading to the end today. Have a great day!

Contact form