How To Query The Price Of A Product Using Shopify Storefront API and GraphQL

5.6k views Asked by At

I'm totally new to both Shopify Storefront API AND GraphQL. I've been pulling my hair out the last 3-4 hours reading through docs and trying to find and write a query that, in my mind anyways, should be simple.

In a nutsell, I have a storefront that has certain products included on it with custom buy buttons. Each buy button's ID corresponds with the product ID on my actual shopify store, meaning ../products/5854987878567

When the page loads, I need to query my store to pull the updated prices, and then reflect those prices in the button text so the person has an accurate idea of how much they are spending.

To do this, I've pulled all unique button ID's and pushed them to a fresh Array, the idea being I can query all ID's at once using GraphQL and get one nice object back that I can then parse and update the buttons accordingly.

After hours of going in circles, I haven't gotten any closer to succeeding.

I've been reading the Query Root documentation and it seems like they do have the products connection, which I assume serves as the entry point to query all products on the store.

However under the query field itself it seems like they have every field BUT id.

available_for_sale
created_at
product_type
tag
title
updated_at
variants.price
vendor 

Are all the fields they have. Since I started writing this I found sortKey, but it failed with the error "Field 'sortKey' doesn't exist on type 'QueryRoot'" sigh.

Here is my query that I'm currently rolling with. I'm not sure this idea will work because if I can only query one id at a time that'll take dozens of API calls per visitor.

$.ajax({
              type : 'POST',
              url : 'https://xxxxx.myshopify.com/api/2020-10/graphql.json',
              headers: {
                'X-Shopify-Storefront-Access-Token': 'xxxxx',
                'Content-Type': 'application/graphql'
              },
              data: 'query products { sortKey(id: "5854987878567") }',
              success: function(res) {
                console.log(res)
              },
              error: function(status) {
                console.log(status)
              }
            });

Any help would be appreciated!

3

There are 3 answers

1
priyanka On

query = ''' { productVariants(first:1,query:"title:%s") { edges { node { id title } } } }
''' % title

make a function and give 'title' as a argument.

def productvariant(title): client = shopify.GraphQL() query = ''' { productVariants(first:1,query:"title:%s") { edges { node { id title } } } }
''' % title

0
priyanka On

you can get the price from product variant just like this - "{
productVariants(first:10, query:"{apply your condition here}") { edges { node { storefrontId compareAtPrice price } } } } % id"

0
Super Kai - Kazuya Ito On

I don't understand what you mean but the GraphQL below gets "id", "title", "description", "the first image" and "price" for each product(10 products):

{
    products(first:10) {
    edges {
      node {
        id
        title
        description
        featuredImage {
          url
        }
        variants(first: 1) {
          edges {
            node {
              priceV2 {
                amount
              }
            }
          }
        } 
      }
    }
  }
}