Have your say in Community Polls: What was/is your greatest motivation to start your own business?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Count SKU of a specific product

Solved

Count SKU of a specific product

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
781 68 112

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

 


 If this fixed your issue, likes and accepting as a solution are highly appreciated
|  Build an online presence with our custom-built Shopify Theme: EcomifyTheme
|  Check out our reviews: Trustpilot Reviews
|  We are Shopify Partners: EcomGraduates Shopify Partner



View solution in original post

Replies 2 (2)

EcomGraduates
Shopify Partner
781 68 112

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

 


 If this fixed your issue, likes and accepting as a solution are highly appreciated
|  Build an online presence with our custom-built Shopify Theme: EcomifyTheme
|  Check out our reviews: Trustpilot Reviews
|  We are Shopify Partners: EcomGraduates Shopify Partner



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