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

构建Shopify Admin UI扩展:如何用GraphQL为折扣关联商品?

Shopify 关联商品到折扣的GraphQL Mutation语句

要在Shopify Admin UI扩展中实现添加商品关联到折扣的功能,你需要根据折扣类型(折扣码/自动折扣)使用对应的GraphQL Mutation:

1. 更新折扣码(Discount Code)并关联商品

使用discountCodeUpdate mutation,通过products.add字段传入要关联的商品ID列表:

mutation DiscountCodeUpdate($discountId: ID!, $products: DiscountProductConnectionInput!) {
  discountCodeUpdate(id: $discountId, discountCode: {products: $products}) {
    discountCode {
      id
      title
      products(first: 10) {
        edges {
          node {
            id
            title
          }
        }
      }
    }
    userErrors {
      field
      message
    }
  }
}

变量示例:

{
  "discountId": "gid://shopify/DiscountCode/456789",
  "products": {
    "add": ["gid://shopify/Product/123456", "gid://shopify/Product/789012"]
  }
}

2. 更新自动折扣(Automatic Discount)并关联商品

如果是自动折扣,使用automaticDiscountUpdate mutation,逻辑类似:

mutation AutomaticDiscountUpdate($discountId: ID!, $products: DiscountProductConnectionInput!) {
  automaticDiscountUpdate(id: $discountId, automaticDiscount: {products: $products}) {
    automaticDiscount {
      id
      title
      products(first: 10) {
        edges {
          node {
            id
            title
          }
        }
      }
    }
    userErrors {
      field
      message
    }
  }
}

关键说明:

  • discountId:目标折扣的全局ID(GID格式),可通过discountCodes或automaticDiscounts查询获取
  • products.add:传入需要关联的商品GID数组,支持一次性添加多个商品
  • 响应中会返回更新后的折扣信息及错误提示(userErrors),用于处理异常情况
  • 确保你的Admin UI扩展已申请write_discounts权限,否则会执行失败

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 05:14:56