How get product by id inside liquid template?

01familia
Excursionist
17 0 4

Hello

I would like to get a product by ID but I don't know how to do that.

The product ID is 2

Please don't tell me "you can use all_products" using this code:

 

 {% for product in collections.all.products %}
       {% if product.id == 2 %}
            {% assign my_product = product %} 
      {% endif %}
 {% endfor %}

 

or:

{{ all_products['2'] }}

 

I cannot use "all_products" because I have many many products (10000 products).
And I cannot create many  many many collections only to get product by id.

Indeed : The all_products object has a limit of 20 unique handles per page. If you want more than 20 products, then consider using a collection instead.

Source: https://shopify.dev/api/liquid/objects#all_products

Screen Shot 2021-07-01 at 11.26.02.png

So, if a shopify employee reads this post (for example an engineer), can you show me the liquid code to get a product by ID?

Thank you so much.

Best Regards 

 

 

Replies 12 (12)

HerculesApps
Shopify Partner
19 1 14

Hello could you find a way to get product by id?

Getting product by handle is not a good way for me, because people can change the handle in the product settings.

HerculesApps
Shopify Partner
19 1 14

 

{% for product in collections.all.products %}
{% if product.id == 6687973638353 %}
{% assign shopifyProduct = product %}
{% endif %}
{% endfor %}

 

This works.

Shadow94
Visitor
2 0 0

Thank you dude !

juanengineered
Shopify Partner
4 0 0

Thank you!

j_on_the_web
Visitor
2 0 0

Does this work with more than 1000 products?  Can we paginate by more than 1000?

Niraj_singh
Shopify Partner
232 39 47

Product by id is good way to get product info and also instead of using "if" please use "case" so best code practice and fast result while searching on loop of around 50 iterations.

{% for product in collections.all.products %}

{%- case product.id -%}
{%- when '123456' -%}

{% assign my_product = product %}
{% endcasae %}
{% endfor %}

banned
juanengineered
Shopify Partner
4 0 0

Good suggestion for switch/case but yours had some typos. The following works for me when inserting as custom liquid block during WYSWYG customization:

{% for product in collections.all.products %}
   {% case product.id %}
      {% when 123456 %}
         {% assign my_product = product %}
   {% endcase %}
{% endfor %}

 

 

Shadow94
Visitor
2 0 0

Hello the community, 

 

How to get a product element from an ID
After much research and help here is the complete solution that works:

 

{% paginate collections.all.products by 1000 %}
                {% for product in collections.all.products %}
                    {% if product.id == 4384952811594 %}
 
                           {% assign shopifyProduct = product %}
 
                   {% endif %}
                {% endfor %}
{% endpaginate %}
 
You need the paginate because shopify limit the hanlder by page.
 

I hope you find it useful.

Take care and good luck

j_on_the_web
Visitor
2 0 0

This increases the limit to 1,000, but what if there are more than 1,000 products?  The initial question mentions 10,000 products.

qjames
Shopify Partner
10 0 9

You can use the 'where' filter on the desired products collection, just make sure you that you cast the id as an integer rather than a string. In this example the id is coming from a text theme-setting, so I cast it to an integer by applying times: 1

 

{%- liquid
  assign product_id = settings.product_id | times: 1
  assign products_obj = collections.all.products | where: 'id', product_id
  echo products_obj[0].handle
-%}

 

 

Hallway
Shopify Partner
19 3 6

Isn't this still subject to the same limit of 1000 products? Or does the "where" filter have some special permission where it'll search the entire collection of more than 1000 products (e.g. if it was 10,000 or more).

Matthew Hall - Founder @ Hallway
Shopify design, build, optimisation, support & consultancy
→ Was my reply helpful? Click Like to let me know!
→ Did I solve your question? Mark it as an Accepted Solution!
oknaus
Shopify Partner
5 0 0

There is still limit.