product variant metafield using storefront api graphql

Solved

product variant metafield using storefront api graphql

Bandile
Visitor
2 1 0

I need to get a products variant metafield and i am able to get the  metafield using the admin graphql api but not using the storefront api this needs to take in a product variant id as an argument 

query = """
{
  productVariant(id: "gid://shopify/ProductVariant/{variant-id}") {
    id
    title
    metafields(first: 5) {
      edges {
        node {
          id
          namespace
          key
          value
        }
      }
    }
  }
}

    """

but struggling the get the same results with the storefont api i do see it the storefront api does not have the same query roots as the admin api so i tried using the node query root as a staring point but im getting errors can you please help me 

{
  node(id: "gid://shopify/ProductVariant/{variant-id}") {
    ... on ProductVariant {
      id
      metafield(key: "product_page", namespace: "buyout_price") {
        id
        key
        value
      }
    }
  }
}

example of the code im using for the storefont version 
Accepted Solution (1)
Bandile
Visitor
2 1 0

This is an accepted solution.

that does not work for the storefront api

const query = `
{
node(id: "${variantId}") {
... on ProductVariant {
id
title
metafield(key: "buyout_price", namespace: "product_page") {
value
id
}
}
}
}
`;

got it though

View solution in original post

Replies 2 (2)

EcomGraduates
Shopify Partner
735 63 105

, it seems that you are trying to retrieve a specific metafield for a product variant using its key and namespace. However, the metafield query for the Storefront API requires the id of the metafield instead.

Here is an example query that should work for you:

 

 

{
  productVariant(id: "gid://shopify/ProductVariant/{variant-id}") {
    id
    title
    metafields(first: 5) {
      edges {
        node {
          id
          key
          namespace
          value
        }
      }
    }
  }
}

 

 

This query should return the metafields for the specified product variant, including their id, key, namespace, and value.

 


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




Bandile
Visitor
2 1 0

This is an accepted solution.

that does not work for the storefront api

const query = `
{
node(id: "${variantId}") {
... on ProductVariant {
id
title
metafield(key: "buyout_price", namespace: "product_page") {
value
id
}
}
}
}
`;

got it though