Get data of a specific product by its ID

Highlighted
Shopify Partner
7 0 0

Hi,

I have a custom page. I need to  display 2 specific products data which I would like to change often. Is there any easy way to get the product informaion of specif product through the Product_Id.

0 Likes
Highlighted
Shopify Partner
1353 24 230

In Liquid you the easiest way to get a product would be trough the handle:

{{ all_products['the-handle'].title }}

To get the ID you need to know the collection and loop trough all the products, max 50, and match the ID in the loop.

https://sections.design tips, tricks & Shopify sections
2 Likes
Highlighted
Shopify Expert
9884 100 1724

If you change the products often, consider creating a menu (linklist) and adding the products as links in that. That way you can loop over the products in that menu and show them however you need.

I'm not sure if you're looking for help with the Liquid or not so post back if you need extra help.

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
0 Likes
Highlighted
Shopify Partner
7 0 0

Hi Guys,
Thanks for your responses.

Actualy my need is to to display product details by its ID.

As we have "product" object in the product detial and collection pages, using that we can get the product Title{{ product.title }}, Decription {{ product.description }}, etc. 

I need to have like this as below.

{% assign product_single = get_product_by_id(8670249347) %}

Titile : {{ product_single.title }}

Description : {{ product_single.description }}

Is there any posibility?

 

0 Likes
Highlighted
Shopify Expert
9884 100 1724

You can not get a single item via the ID as you can will the all_products[handle] method Mircea noted above. There's no all_products[ID] equivalent.

What's the actual usage here that is forcing you to need the ID instead of the handle within liquid? One would think that if you have the ID, you would also be able to use the handle instead. 

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
1 Like
Shopify Partner
7 0 0

Jason and Mircea,

Thaks for your help.

Now I got it. I did my task using all_products[handle] .

0 Likes
Highlighted
Shopify Partner
1353 24 230

The get product by ID is quite often asked. I wonder why that is not an option.

Niresh, the all_products[handle] should do the job, the handle is quite similar to an ID, unique per store. As Jason mentioned, if you have the ID you should also have the handle. The limitation is that you can pull a maximum of 20 products like that.

 

https://sections.design tips, tricks & Shopify sections
2 Likes
Highlighted
Shopify Partner
7 0 0

Thanks Mircea. Now i am clear with this.

0 Likes
Highlighted
Shopify Partner
3 0 9

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 %}

9 Likes
Highlighted
Tourist
7 0 6

Exactly! Using the handle is crazy because it is editable. The only reason I'm on this page is because our relationships broke when a colleage edited URLS (handles). IDs never change and are the only truly stable option.

Shopify: PLEASE ADD "GET PRODUCT BY ID"!!!

5 Likes