Shopify: Find Product by Title

Template
Function
1
Template
Function
by
Voiceflow Community

Get product information by using the product title.

Created:

Heading

Voiceflow APIs used:

Channels
No items found.
Created By
Voiceflow Community
This is some text inside of a div block.
Overview
This is some text inside of a div block.
by
This is some text inside of a div block.

Heading 1

Heading 2

Heading 3

Heading 4

Heading 5
Heading 6

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Block quote

Ordered list

  1. Item 1
  2. Item 2
  3. Item 3

Unordered list

  • Item A
  • Item B
  • Item C

Text link

Bold text

Emphasis

Superscript

Subscript

Heading
Based in
This is some text inside of a div block.
Heading

Function Code Snippet


export default async function main(args) {
const { shopifyStoreName, shopifyApiVersion, shopifyAdminApiKey, productTitle } = args.inputVars;

// Validate input variables
if (!shopifyStoreName || !shopifyApiVersion || !shopifyAdminApiKey || !productTitle) {
return {
next: { path: 'error' },
trace: [{ type: "debug", payload: { message: "Missing required input variables" } }]
};
}

const url = `https://${shopifyStoreName}.myshopify.com/admin/api/${shopifyApiVersion}/products.json?limit=250`;
const headers = {
'X-Shopify-Access-Token': shopifyAdminApiKey,
'Content-Type': 'application/json'
};

try {
// Fetch the list of products
const response = await fetch(url, { method: 'GET', headers: headers });
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const responseBody = await response.json;

// Check if the response contains products
if (!responseBody.products || !Array.isArray(responseBody.products)) {
throw new Error("Invalid or missing response body from Shopify API");
}

// Find the product by title
const product = responseBody.products.find(p => p.title.toLowerCase() === productTitle.toLowerCase());

if (!product) {
return {
next: { path: 'no_match' },
trace: [{ type: "debug", payload: { message: "No matching product found" } }]
};
}

// Extract the required values
const productId = product.id;
const productType = product.product_type;
const bodyHtml = product.body_html;
const adminGraphqlApiId = product.admin_graphql_api_id;
const firstVariant = product.variants[0];
const firstVariantPrice = firstVariant.price;
const inventoryQuantity = firstVariant.inventory_quantity;
const option1 = firstVariant.option1;
const option2 = firstVariant.option2;

return {
outputVars: {
productId,
productType,
firstVariantPrice,
bodyHtml,
adminGraphqlApiId,
inventoryQuantity,
option1,
option2
},
next: { path: 'success' },
trace: [{ type: "debug", payload: { message: "Product found and values extracted" } }]
};

} catch (error) {
return {
next: { path: 'error' },
trace: [{ type: "debug", payload: { message: `Error: ${error.message}` } }]
};
}
}
copy-icon

Explore More Templates

Build and submit a Template to have it featured in the community.

ghraphic
No items found.
No items found.