A space to discuss GraphQL queries, mutations, troubleshooting, throttling, and best practices.
How to count number of SKU under a product? is there any API call?
Thank you,
Kamal
Solved! Go to the solution
This is an accepted solution.
hello there
Make a GET request to the Shopify API endpoint for the product you're interested in. The endpoint URL should look like this: /admin/api/2021-09/products/{product_id}.json
, where {product_id}
is the ID of the product you want to retrieve.
Parse the response to extract the variants
array, which contains an object for each SKU associated with the product.
Get the length of the variants
array to determine how many SKUs are associated with the product.
Here's some example code in JavaScript that demonstrates how to do this using the node-fetch
library:
const fetch = require('node-fetch');
const shopifyDomain = 'your-shopify-domain.myshopify.com';
const accessToken = 'your-access-token';
const productId = '123456789'; // replace with the ID of the product you want to count SKUs for
const url = `https://${shopifyDomain}/admin/api/2021-09/products/${productId}.json`;
const options = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'X-Shopify-Access-Token': accessToken,
},
};
fetch(url, options)
.then(res => res.json())
.then(json => {
const variants = json.product.variants;
const numVariants = variants.length;
console.log(`The product has ${numVariants} SKUs`);
})
.catch(err => console.error(err));
This is an accepted solution.
hello there
Make a GET request to the Shopify API endpoint for the product you're interested in. The endpoint URL should look like this: /admin/api/2021-09/products/{product_id}.json
, where {product_id}
is the ID of the product you want to retrieve.
Parse the response to extract the variants
array, which contains an object for each SKU associated with the product.
Get the length of the variants
array to determine how many SKUs are associated with the product.
Here's some example code in JavaScript that demonstrates how to do this using the node-fetch
library:
const fetch = require('node-fetch');
const shopifyDomain = 'your-shopify-domain.myshopify.com';
const accessToken = 'your-access-token';
const productId = '123456789'; // replace with the ID of the product you want to count SKUs for
const url = `https://${shopifyDomain}/admin/api/2021-09/products/${productId}.json`;
const options = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'X-Shopify-Access-Token': accessToken,
},
};
fetch(url, options)
.then(res => res.json())
.then(json => {
const variants = json.product.variants;
const numVariants = variants.length;
console.log(`The product has ${numVariants} SKUs`);
})
.catch(err => console.error(err));
Hello ,
Thank you very much for this brilliant idea !!
I forgot that already listing out product details sending API call and there have
multiple variants associated with it's SKUs
Your idea & solution should work.
Thank you again,
Kamal Hossain