You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Shopify GraphQL按位置获取指定集合的可用商品

解决Shopify GraphQL按位置筛选商品集合内商品的问题

问题描述

需求:通过Shopify GraphQL获取指定商品集合中基于特定位置的商品列表,期望得到包含对应库存信息的商品列表。

尝试的查询代码:

query($collectionId: ID!, $locationId: ID!) {
  collection(id: $collectionId) {
    products(first: 50) {
      edges {
        node {
          id
          title
          availableForSale
          images(first: 50) {
            edges {
              node {
                id
                altText
                originalSrc
              }
            }
          }
          variants(first: 50, query: "location_id:$locationId") {
            edges {
              node {
                id
                title
                inventoryQuantity
                sku
              }
            }
          }
        }
      }
    }
  }
}

执行报错:提示variants字段不支持query参数。

解决方案

Shopify的GraphQL API中,Product.variants字段确实没有query参数可用,要按位置筛选库存,需要改用inventoryItem关联的inventoryLevels字段来获取指定位置的库存信息。

修改后的查询代码:

query($collectionId: ID!, $locationId: ID!) {
  collection(id: $collectionId) {
    products(first: 50) {
      edges {
        node {
          id
          title
          availableForSale
          images(first: 50) {
            edges {
              node {
                id
                altText
                originalSrc
              }
            }
          }
          variants(first: 50) {
            edges {
              node {
                id
                title
                sku
                inventoryItem {
                  inventoryLevels(first: 1, query: "location_id:$locationId") {
                    edges {
                      node {
                        inventoryQuantity
                        location {
                          id
                          name
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

说明

  • 移除了variants上的query参数,转而通过variant.inventoryItem.inventoryLevels来筛选指定位置的库存数据
  • inventoryLevels支持使用query参数按location_id过滤,这样就能拿到对应位置的库存数量
  • 如果商品在该位置没有库存,inventoryLevels返回的列表会是空的,可以据此判断商品在目标位置的可用性

内容的提问来源于stack exchange,提问作者Sandrik85

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 10:01:40