All things Shopify and commerce
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
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
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.
{% for product in collections.all.products %}
{% if product.id == 6687973638353 %}
{% assign shopifyProduct = product %}
{% endif %}
{% endfor %}
This works.
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 %}
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 %}
Hello the community,
How to get a product element from an ID
After much research and help here is the complete solution that works:
I hope you find it useful.
Take care and good luck
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
-%}
Does this work with more than 1000 products? Can we paginate by more than 1000?
This increases the limit to 1,000, but what if there are more than 1,000 products? The initial question mentions 10,000 products.
User | RANK |
---|---|
68 | |
49 | |
36 | |
34 | |
29 |
Learn these 5 things I had to learn the hard way with starting and running my own business
By Kitana Jan 27, 2023Would you love to unleash the unbridled power of the Google Shopping Channel into your sho...
By Gabe Jan 6, 2023How can you turn a hobby into a career? That’s what Emmanuel did while working as a wa...
By Skye Dec 30, 2022