Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Product variants error

Solved

Product variants error

dripdevelopment
Shopify Partner
11 0 0

Why am I getting always only the last variant of a product, even if I specify to get more than 1 (e.g. first: 3)?

I've tried a couple of things, the first was directly fetching variants by searching variants with the same product_id, and I've also tried fetching all the variants in a product query (e.g. query{products(first: 10){edges{ node{ variants(first:10){edges{node{}}}}}}} )

Accepted Solution (1)

LucaRaveri
Shopify Partner
20 3 7

This is an accepted solution.

Hey @dripdevelopment,

I think you should provide more details about your issue.

I tried this query and everything works fine:

query {
  products(first: 10) {
    edges {
      node {
        title
        variants(first: 10) {
          edges {
            node {
              id
              title
            }
          }
        }
      }
    }
  }
}

 

If this was helpful, please Like it ⬆️
If this solved your problem, please Mark it as solution

View solution in original post

Replies 6 (6)

LucaRaveri
Shopify Partner
20 3 7

This is an accepted solution.

Hey @dripdevelopment,

I think you should provide more details about your issue.

I tried this query and everything works fine:

query {
  products(first: 10) {
    edges {
      node {
        title
        variants(first: 10) {
          edges {
            node {
              id
              title
            }
          }
        }
      }
    }
  }
}

 

If this was helpful, please Like it ⬆️
If this solved your problem, please Mark it as solution
dripdevelopment
Shopify Partner
11 0 0

Thank you for your response. I have already contacted support regarding this issue but have not yet received a reply. The problem I'm having is that, even if certain product has more than 1 variant, I always get the latest one and I can't find a way to fetch all the variants from specific product.
e.g. I only get 1 variant (the one with the position #2)

query {
  productVariants(first: 10, query: "product_id:9670723305788") {
    edges {
      node {
        id
        position
        price
        sku
        inventoryQuantity
        inventoryPolicy
        image{
            url
        }
        createdAt
        updatedAt
      }
    }
  }
}

Response

{
    "data": {
        "productVariants": {
            "edges": [
                {
                    "node": {
                        "id": "gid://shopify/ProductVariant/50019571728700",
                        "position": 2,
                        "price": "12.00",
                        "sku": "",
                        "inventoryQuantity": 21,
                        "inventoryPolicy": "DENY",
                        "image": null,
                        "createdAt": "2024-09-02T17:57:50Z",
                        "updatedAt": "2024-09-02T17:57:50Z"
                    }
                }
            ]
        }
    },
    "extensions": {
        "cost": {
            "requestedQueryCost": 10,
            "actualQueryCost": 4,
            "throttleStatus": {
                "maximumAvailable": 2000.0,
                "currentlyAvailable": 1996,
                "restoreRate": 100.0
            }
        }
    }
}
LucaRaveri
Shopify Partner
20 3 7

Hey @dripdevelopment, I think you are doing something wrong here, if you look at the documentation of the ProductVariant Object you see you can get a variant from the variant id or you can get a variants list that is not something related to a single product. 
https://shopify.dev/docs/api/admin-graphql/2024-07/objects/ProductVariant#queries

I'm curious, can you please provide me the documentation about this filter you are using? product_id:9670723305788


However, what you should do instead is something like this:

query getProductWithVariants($productId: ID!) {
  product(id: $productId) {
    id
    variants(first: 100) {
      edges {
        node {
          id
          position
          price
          sku
          inventoryQuantity
          inventoryPolicy
          image {
            url
          }
          createdAt
          updatedAt
        }
      }
    }
  }
}

 

If this was helpful, please Like it ⬆️
If this solved your problem, please Mark it as solution
dripdevelopment
Shopify Partner
11 0 0

Thanks for the provided query, but I'm still getting just 1 variant. I'd expect to get all the variants from a specific product if I provide the product_id (as specified in the docs), like it happens in REST Admin

LucaRaveri
Shopify Partner
20 3 7

Hey @dripdevelopment,

thank you for providing the documentation, I tried it and that actually works. I specified an id of a product with 6 variants and i got back 6 variants. 
If you try my query do you get more than one variant?

If this was helpful, please Like it ⬆️
If this solved your problem, please Mark it as solution
dripdevelopment
Shopify Partner
11 0 0

Good to know that you're getting more than 1 variant, perhaps it's an issue related to my account. I tried your query, but it only returns 1 variant where there should be 2 at least.