Get data of a specific product by its ID

Highlighted
New Member
3 0 0

Hi guys this discussion was very helpful.

Just one question: when I use Mircea tag it only shows me the product title.

I'm trying to show products in a collection by handle (32 per page) with handles: hafdsl_lightbulbsxxx

How can I do that?

 

Thanks

0 Likes
Highlighted
Excursionist
22 0 5

+1 not being able to get product by ID is crazy.

This is a 2016 post and its not been fixed yet!

BUMP!

4 Likes
Highlighted
Tourist
9 0 3

I agree, I don't know why it's not been added really. There must be a reason I'm sure?

0 Likes
Highlighted
New Member
1 0 0

I am not sure if this will help.

 

but this is how i got the id of my item. 

 

{% assign variant = all_products[handle].variants.first.id %}

{{ variant }}

 

My product structure contains variants (yours might not). The all_products grabs the (main) product by handle, then i drill down into the object for the variants. Variants return an array. So I use the "first" method to grab just the first variant (only one I need) in the array. Then grab I grab the ID. 

0 Likes
Highlighted
Shopify Partner
129 7 30

Here's a common use case for this with no solution. You can store handles in a metafield for use as related products or links or whatever. If the handle changes, there's no way whatsoever to load the product from that old handle. The all_products object returns empty, even though a link to it would redirect to the correct place. Doesn't help things that there's no way to load a product object by its ID so you can avoid this entirely. Still more frustrating is that there's no way to query metafields by their values so you can't listen for changes and correct them as they happen. All dead ends with zero workarounds. 

2 Likes
Highlighted
Shopify Partner
35 0 6

I have the same problem now.  I set up a related-products section with related products specified by product tag (e.g., "relatedProductPosition101:the-related-products-handle-goes-here").  We've found that the product search/filter in the admin now returns products that have our search query in the tags.  So, if I search for part of the handle of product A, and product B has A's handle in a related-products tag, I get both results.  Since popular products are in tags on many other products, this has caused quite a mess.

 

Shopify, is there any chance of revisiting the need to get all_products by ID in addition to just by handle?

 

@Jason_Jaeger wrote:

I also would love to be able to get products (and collections for that matter) by their ID.

{% rant %}

The fact that I cannot get a product or collection by it's ID in either liquid or the javascript API seems CRAZY to me.

There are many times that I want to create a relationship between two entities (upsells, cross-sells, main collection, parent collection, etc etc etc). I would LOVE to use meta fields to store the IDs of the referenced entities (be it a product/variant or collection). 

The main problem with using handles is: if you use a handle and a website admin decides they want a different URL for whatever reason, they change the handle and your relationship is broken. This does not seem like an acceptable way to reference an entity to me.
{% endrant %}


 

4 Likes
Highlighted
Shopify Partner
31 1 13

+1 for this

2 Likes
Highlighted
Tourist
11 0 2

They're too busy making robots

0 Likes
Highlighted
Excursionist
17 0 2

@Jason  - I have handle in cart items and m not able to fetch product data from the cart items handle. how can I do that.

0 Likes