Get Products

An easy way to get multiple products details directly from Shopify

Description

The getProducts helper retrieves multiple Shopify products in a single call, based on collection ID or collection handle. It provides a paginated list of products with core information — useful for PLPs, carousels, and recommendation blocks.

Function Signature

VajroSDK.helpers.getProducts({
  collectionHandle:"men",//handle of the collecton from Shopify  
  count: 10 //Number of product you want to fetch
})
  .then((data) => {
    console.log("data")
    const products = data.products || data;
  })
  .catch((err) => {
    console.error("Error fetching products:", err);
  });

																			(OR)

VajroSDK.helpers.getProducts({
  collectionId:"12346789", //ID of the collecton from Shopify       
  count: 10 //Number of product you want to fetch
})
  .then((data) => {
    console.log("data")
    const products = data.products || data;
  })
  .catch((err) => {
    console.error("Error fetching products:", err);
  });

We don't support metafields, market based filters and inventory based on location

ParameterTypeRequiredDescription
idStringYes (if handle is not passed)ID of the collection available in Shopify
handleStringYes (if id is not passed)Handle of the collection available in Shopify
countNumberYesNumber of products you want to fetch from the collection

Structure of Response

An array of products with each product having its properties.

Sample response structure of each product is mentioned below

PropertyTypeDescriptionExample
idStringUnique ID of each product"5678767788e"
titleStringName of each productMen shoes
descriptionStringHTML content of each product"men shoes ..."
handleStringSlug of each product used in URL"men-shoes"
featuredImageStringThe main image associated to each product"https://cdn.shopify.com/.../hoodie.jpg"
imagesArrayArray of images associated to each product[ "https://cdn.shopify.com/.../hoodie.jpg", "https://cdn.shopify.com/.../hoodie.jpg" ]
variantsObject[]An object containing all the properties of the variant of each product[{"id": "1234","title":"grey"}]
selectedOptionsObject[]An object containing all option sets available for each product[{"name": "Size","value":"xl"}]
vendorStringVendor name of each product"CONVERSE"

Example Usage

<div id="productGrid" class="product-grid"></div>
.product-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 16px;
}

.product-card {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.2s;
}

.product-card:hover {
  transform: scale(1.05);
}

.product-image {
  width: 100%;
  height: 150px;
  object-fit: cover;
}

.product-info {
  padding: 8px;
  text-align: center;
}

.product-name {
  font-size: 14px;
  font-weight: 600;
  margin: 8px 0;
}

.product-price {
  font-size: 12px;
  color: #333;
}
// Function to fetch products from a specific collection
function fetchProductsFromCollection(collectionId) {
    VajroSDK.helpers.getProducts({
        collectionId: collectionId,
        count: 10 // Fetch 10 products
    })
    .then((data) => {
        const products = data.products || data;
        displayProducts(products);
    })
    .catch((err) => {
        console.error("Error fetching products:", err);
    });
}

// Function to display products in a grid
function displayProducts(products) {
    const productGrid = document.getElementById('productGrid');
    productGrid.innerHTML = ''; // Clear existing products

    products.forEach(product => {
        const card = document.createElement('div');
        card.className = 'product-card';
        card.innerHTML = `
            <img src="${product.images[0]}" alt="${product.title}" class="product-image" />
            <div class="product-info">
                <div class="product-name">${product.title}</div>
                <div class="product-price">₹${product.variants[0].price}</div>
            </div>
        `;
        productGrid.appendChild(card);
    });
}

// Call the function with a specific collection ID
fetchProductsFromCollection("324840718521");

Best Practices

  1. Avoid overfetching: Always limit product count to optimize performance.
  2. Use consistent display logic: Match product card design to PDP for consistent UX.
  3. Graceful fallback: If no products are returned, display a “No items found” message.

Caveats

  1. Partial data: Product metafields and deep relationships are not included for performance reasons.
  2. Read-only: The helper retrieves products; it does not support modifications.
  3. Null safety: Always check if response.products is defined before accessing it.