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
| Parameter | Type | Required | Description |
|---|---|---|---|
| id | String | Yes (if handle is not passed) | ID of the collection available in Shopify |
| handle | String | Yes (if id is not passed) | Handle of the collection available in Shopify |
| count | Number | Yes | Number 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
| Property | Type | Description | Example |
|---|---|---|---|
| id | String | Unique ID of each product | "5678767788e" |
| title | String | Name of each product | Men shoes |
| description | String | HTML content of each product | "men shoes ..." |
| handle | String | Slug of each product used in URL | "men-shoes" |
| featuredImage | String | The main image associated to each product | "https://cdn.shopify.com/.../hoodie.jpg" |
| images | Array | Array of images associated to each product | [ "https://cdn.shopify.com/.../hoodie.jpg", "https://cdn.shopify.com/.../hoodie.jpg" ] |
| variants | Object[] | An object containing all the properties of the variant of each product | [{"id": "1234","title":"grey"}] |
| selectedOptions | Object[] | An object containing all option sets available for each product | [{"name": "Size","value":"xl"}] |
| vendor | String | Vendor 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
- Avoid overfetching: Always limit product count to optimize performance.
- Use consistent display logic: Match product card design to PDP for consistent UX.
- Graceful fallback: If no products are returned, display a “No items found” message.
Caveats
- Partial data: Product metafields and deep relationships are not included for performance reasons.
- Read-only: The helper retrieves products; it does not support modifications.
- Null safety: Always check if response.products is defined before accessing it.
Updated about 1 month ago