【问题标题】:Shopify GraphQL: How to add multiple variants to a product?Shopify GraphQL:如何为产品添加多个变体?
【发布时间】:2021-10-05 14:48:51
【问题描述】:

我正在关注 GraphQL documentation for `productCreate,但我不知道如何在创建产品时添加多个变体。

我正在使用以下突变:

mutation productCreate($input: ProductInput!) {
  productCreate(input: $input) {
        userErrors {
      field
      message
    }
    shop {
      id
    }
    product {
      title
      handle
      variants {
        edges {
          node {
            title
          }
        }
      }
    }
  }
}

并传递以下变量:

{
 "input": {
    "title": "Testing Products",
    "handle": "test-product-2",
    "variants": [
        { "title": "Variant 1" },
      { "title": "Variant 2" }
    ]
    }
}

我得到的错误是:

{
  "data": {
    "productCreate": {
      "userErrors": [
        {
          "field": [
            "variants",
            "1"
          ],
          "message": "The variant 'Default Title' already exists."
        }
      ],
      "shop": {
        "id": "gid://shopify/Shop/59898691733"
      },
      "product": null
    }
  },
  "extensions": {
    "cost": {
      "requestedQueryCost": 12,
      "actualQueryCost": 10,
      "throttleStatus": {
        "maximumAvailable": 1000,
        "currentlyAvailable": 990,
        "restoreRate": 50
      }
    }
  }
}

我搜索了互联网,试图弄清楚如何在 Shopify 上创建产品时添加多个变体,但我无法弄清楚。

可以这样做吗?

【问题讨论】:

    标签: graphql shopify


    【解决方案1】:

    我错误地定义了变体。如果没有 options 键(奇怪的是它是复数,考虑到它只需要一个字符串?)它将无法工作。

    {
     "input": {
        "title": "Testing Products",
        "handle": "test-product-2",
        "variants": [
            { 
            "title": "Size Small",
            "options": "Small"
          },
          { 
            "title": "Size Medium",
            "options": "Medium"
          },
          { 
            "title": "Size Large",
            "options": "Large"
          }
        ]
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-31
      • 1970-01-01
      • 2017-11-19
      • 2016-08-13
      • 1970-01-01
      • 2021-07-06
      • 1970-01-01
      • 2020-01-11
      相关资源
      最近更新 更多