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

如何通过Shopify GraphQL获取特定折扣码对应的产品变体ID

如何通过Shopify GraphQL检索特定折扣码适用的产品变体ID

你当前的查询没有展开customerGets.items的具体字段,无法获取产品或变体ID。要拿到目标数据,需要针对折扣适用的商品类型(产品/变体),在查询中添加对应的类型片段,展开ID字段。

修改后的GraphQL查询

query codeDiscountNodeByCode($code: String!) {
  codeDiscountNodeByCode(code: $code) {
    id
    codeDiscount {
      ... on DiscountCodeBasic {
        status
        customerGets {
          items {
            __typename
            # 针对适用整款产品的折扣,获取产品ID及旗下变体ID
            ... on DiscountItemProduct {
              product {
                id
                variants(first: 100) { # 可根据需求调整first参数,最大支持250
                  edges {
                    node {
                      id
                    }
                  }
                }
              }
            }
            # 针对特定变体的折扣,直接获取变体ID
            ... on DiscountItemVariant {
              variant {
                id
              }
            }
          }
        }
        combinesWith {
          productDiscounts
        }
        minimumRequirement {
          __typename
          ... on DiscountMinimumQuantity {
            greaterThanOrEqualToQuantity
          }
        }
        title
        shortSummary
        summary
        discountClass
        customerSelection {
          __typename
        }
      }
    }
  }
}

关键说明

  • customerGets.items是多类型字段,会根据折扣规则返回DiscountItemProduct(适用整产品)、DiscountItemVariant(适用特定变体)或DiscountItemAll(全店通用),需通过__typename识别类型并展开对应字段。
  • 若返回DiscountItemAll,说明该折扣适用于全店商品,无特定产品/变体ID。
  • 变体列表的first参数可按需调整,若变体数量超过250,需使用分页参数after获取剩余数据。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 08:40:20