Count SKU of a specific product

Solved
khossain
Excursionist
18 2 4

How to count number of SKU under a product? is there any API call?

 

Thank you,

Kamal

Accepted Solution (1)
EcomGraduates
Shopify Partner
588 48 73

This is an accepted solution.

hello there  

 

  1. 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.

  2. Parse the response to extract the variants array, which contains an object for each SKU associated with the product.

  3. 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));

 

banned

View solution in original post

Replies 2 (2)
EcomGraduates
Shopify Partner
588 48 73

This is an accepted solution.

hello there  

 

  1. 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.

  2. Parse the response to extract the variants array, which contains an object for each SKU associated with the product.

  3. 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));

 

banned
khossain
Excursionist
18 2 4

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