如何通过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
相关产品推荐
相关产品推荐

