Focusing on managing products, variants, and collections through the API.
Hi,
We build custom page for warranty system, we need to get Product name from product SKU, is there any API or liquid code available to get product name?
Solved! Go to the solution
This is an accepted solution.
Hi Niku0911,
You can get the product name from the product SKU using the Admin GraphQL API. The API allows you to search for a product variant using its SKU and then retrieve the associated product name.
Here is an example of how you might structure your GraphQL query:
{
productVariants(first: 1, query: "sku:<YOUR_SKU>") {
edges {
node {
product {
title
}
}
}
}
}
This query looks for the first product variant that matches the provided SKU and then returns the associated product's title.
As for Liquid, there is no direct way to get a product based on a SKU. One option you could explore with Liquid would be to use the new Metaobjects Pages feature to build the custom warranty page. This allows you to set up page templates that have access to custom fields and data. So could create metaobject entries for each product and include a field for the title which would be accessible on the metaobject page. It's worth exploring this option, there's a great video here that shows the process of working with metaobject pages.
Hope this helps!
Liam | Developer Advocate @ Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog
This is an accepted solution.
Hi Niku0911,
You can get the product name from the product SKU using the Admin GraphQL API. The API allows you to search for a product variant using its SKU and then retrieve the associated product name.
Here is an example of how you might structure your GraphQL query:
{
productVariants(first: 1, query: "sku:<YOUR_SKU>") {
edges {
node {
product {
title
}
}
}
}
}
This query looks for the first product variant that matches the provided SKU and then returns the associated product's title.
As for Liquid, there is no direct way to get a product based on a SKU. One option you could explore with Liquid would be to use the new Metaobjects Pages feature to build the custom warranty page. This allows you to set up page templates that have access to custom fields and data. So could create metaobject entries for each product and include a field for the title which would be accessible on the metaobject page. It's worth exploring this option, there's a great video here that shows the process of working with metaobject pages.
Hope this helps!
Liam | Developer Advocate @ Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog
Hi Liam,
thanks, i used this GraphQL API.
Hello @Liam
What if I want to get the product title as well as its ID, inventory_item_id, variant_id, and price?
Hi @Av18
You should be able to extend the GraphQL Query with the information you want.
Under node you can add the "productVariant fields", and "product" related fields can be looked up under "product".
Lookup the fields and connections here: https://shopify.dev/docs/api/admin-graphql/2024-01/objects/ProductVariant#fields
So something like this:
{
productVariants(first: 1, query: "sku:<YOUR_SKU>") {
edges {
node {
id
inventoryItem {
id
}
price
product {
id
title
}
}
}
}
}
Hey Liam,
I'm facing similar problem keep getting 404 Not Found error, using SKU to get product handle.
I use Express.js, and below is my code:
const SHOPIFY_SHOP = process.env.SHOPIFY_SHOP;
const ACCESS_TOKEN = process.env.SHOPIFY_ACCESS_TOKEN;
const getProductHandleBySKU = async (sku) => {
const query = `
query getProductHandleBySKU($sku: String!) {
productVariants(first: 1, query: "sku:${sku}") {
edges {
node {
product {
handle
}
}
}
}
}
`;
const variables = { sku: `${sku}` };
try {
console.log(`Sending query for SKU: ${sku}`); // Log the SKU being queried
const response = await axios({
url: `https://${SHOPIFY_SHOP}.myshopify.com/admin/api/2024-07/graphql.json`,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Shopify-Access-Token': ACCESS_TOKEN,
},
data: JSON.stringify({ query, variables }),
});
// Check if response contains product handle
const productHandle = response.data.data?.productVariants?.edges?.[0]?.node?.product?.handle;
if (productHandle) {
console.log(`Found product handle: ${productHandle} for SKU: ${sku}`);
return productHandle;
} else {
console.log(`No product found for SKU: ${sku}`);
return null;
}
} catch (error) {
if (error.response) {
console.error(`Error fetching product handle for SKU: ${sku}`);
console.error('Error Response:', error.response.data);
console.error(`Status: ${error.response.status} | StatusText: ${error.response.statusText}`);
} else {
console.error('Error Message:', error.message);
}
return null;
}
};