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 7

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
Excursionist
12 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
Highlighted

+1 for this too. At least we'd like to know whether there is a technical reason why it's not done, or whether since 2016 they just don't bother reading topics and feedback. Are you listening Shopify ?

Also in some case (depends on your project), instead of storing handle in metafields, you can store the handle in the theme settings that merchants can access using the theme editor. The method I will share here only works if you want to store and use product IDs based on the current/visited product page ID. For my example, I did a product bundle custom section on the product template, and I need to access other product IDs in order to loop through their variants. So if your use case isn't similar, bellow example won't help you.

To start with, I use the blocks as product identifiers. This way we can assign one block for one product page and are not limited. To do so, I get the index of the block that corresponds to the product page I'm visiting : 

 

{% for block in section.blocks %} 
{% assign block_product_id = block.settings.product_id | plus: 0 %}
// product_id is "text" attribute where user inputs the product ID he wants to configure. Plus: 0 makes it an integer.
// I do not advise using "product" attribute (to get the ID) at this point because of the 20 limit.
{% if block_product_id == product.id %}
{% assign index = forloop.index0 %}
{% endif %}
{% endfor %}

 

Now, you are free to put whatever you want inside this block. In my use case, I want the user to pick 2 other products in order to build bundle pack based on the current product page. So I use the product attribute twice and I end up with 2 product handles, individually picked for one product page.

Then I can access the handles and loop through the product options for example. 

 

{% capture product_handle_1 %}{{ section.blocks[index].settings.product_upsell_1 }}{% endcapture %}
{% capture product_handle_2 %}{{ section.blocks[index].settings.product_upsell_2 }}{% endcapture %}
// [index] is the block index we retrieved in previous step
// product_upsell_1 and product_upsell_2 are "product" attributes I need for my bundle

{% assign upsell_product_1 = all_products[product_handle_1] %}
{% assign upsell_product_2 = all_products[product_handle_2] %}

{% for option in upsell_product_1.options_with_values %}
//do my things for product bundle 1...
{% endfor %}
{% for option in upsell_product_2.options_with_values %}
//do my things for product bundle 2...
{% endfor %}

 

You could also get the ID and do whatever with it. This way you do not risk to end up with a dead handle like with metafields. I know this is quite a specific use case, I just posted in case it helps someone. Happy liquid coding !

C'est simple comme bonjour ! Ou presque.
Need some help to customize your website? Message me anytime: 5euros.com/profil/maxdesign
0 Likes