Hairgami_Master
Hairgami_Master

Reputation: 5539

Getting current Product ID with Shopify API for a Javascript API call

I'm toying with developing a Shopify App that uses the DaisyAPI dynamic image service for Shopify. In order to make the correct call to the Daisy API, I need to know what Product the customer is looking at. It seems like, to reliably do that, I will need to manipulate their product.liquid theme to insert a hidden value containing the Product ID so that Jquery can pick up that value and insert it into the API call.

What's the best way to ensure every product page contains the product ID in such a way jQuery can reliably find it? Am I overlooking something obvious?

Many thanks!

Upvotes: 9

Views: 6952

Answers (2)

John Duff
John Duff

Reputation: 38598

Another option would be to use the Ajax API to request the product. Of you know you're on a product page simple make a request to the same URL and append .json. You will then have a json representation of the product that you can do whatever yo want with.

To get you js into the theme you can use a script tag which will load js from you server onto the shop.

Upvotes: 17

David Lazar
David Lazar

Reputation: 11425

You are missing a very obvious and cool Liquid filter.

In Liquid, pump the Object for it's JSON.

var p = {{ product | json }};

And then go to town... you cannot go far on the client without this concept. Get the JSON man.. get the JSON...

Upvotes: 13

Related Questions