0
votes

I've created a JQuery script that has multiple selector forms to narrow down a complicated multi-sku product in Shopify.

By the time the customer has selected everything, the shopify variant IDs are returned as variables via an array (not getting data directly from Shopify).

e.g.

var idone = "15065378226219"
var idtwo = "13367973249067"

Now I want to use these values to get the variant inventory quantity. I've tried this code

function getValues(callback) {
   $.getJSON("/admin/variants/15065378226219.json",function(result){
     callback(result);    // this should be the return value
   });
}


 getValues(function(values) {
   alert(values);
}); 

But the alert doesn't do anything, so I don't think anything's happening. My goal is to get the variant inventories for both IDs so I can validate in JS whether or not the product is available (both IDs have inventory greater than 0).

Any guidance would be appreciated!

Thanks

1
If that is client side code, running on what might be a customers computer on the front-facing part of your site, '/admin'-type urls probably won't work at all.TKoL
A url like this will be what you want to use probably: ''/products/<product-handle>.js''TKoL
I see – is it therefore possible to go directly to variant or will I have to call the parent product data then search for the variant ID I need?Tbizzle
Probably have to get the whole parent product and pull the variant data outTKoL
It's not possible to get a variant directly through the front-end. You'll have to get the data through the parent product - though on the upside, if most of your variants belong to the same product, you'll be saving a few AJAX calls if you're getting all the data at onceDave B

1 Answers

0
votes

When you access your complex product in Liquid, there is a filter that shows the product off as JSON. Amazingly, this filter is 'json'. So try this Liquid:

var fizzborked = {{ product | json }};

You can inspect that in a comment, or however you wish. Look for your variants. Note the inventory quantity being there. Use that to build your logic. You can no longer make callbacks to Shopify for inventory amounts. 10 years ago people were complaining about having their inventory levels scraped, so Shopify cut that off.

As far as more sophisticated approaches go, you could always use a private App to support a Proxy call, allowing you to make secure Ajax calls to cull whatever data you need to support your use-case. Win Win!