Cart API V3: Can't create a Cart for product that has options

独自空忆成欢 提交于 2019-12-04 06:12:52

问题


When I create a cart with products without options, everything works fine, but if any of the products has product option, it doesn't work Here I got the product options, it has one option with id 21

When I use this option id in creating the API, it doesn't work


回答1:


If you are adding a product to the cart that has a single modifier associated with it (like a text field) try the POST to the cart API without including the "variant_id" field:

{
  "line_items": [
    {
      "quantity": 1,
      "product_id": 1001,
      "option_selections": [
        {
          "option_id": 123,
          "option_value": "Hello!"
        }
      ]
    }
  ]
}

If your product has one option (like a radio button) associated with it, try this request, using just the variant ID to identify the option:

{
  "line_items": [
    {
      "quantity": 1,
      "product_id": 1001,
      "variant_id": 2331
    }
]
}

If your product has both an option (radio button) and a modifier (text field), this sample request should work. The first option selection corresponds to the radio button option and the second option selection corresponds to the text field modifier. No variant id is included:

{
  "line_items": [
    {
      "quantity": 1,
      "product_id": 101,
      "option_selections": [
        {
          "option_id": 231,
          "option_value": 456
        },

        {
          "option_id": 123,
          "option_value": "Hello!"
        }
      ]
    }
  ]

For context on the v3 terminology, both options and modifiers are terms for lists of choices attached to products, but options are choices that are used to build out variants (SKUs) and modifiers are choices that are not tied to variants at all. This is why a text field would be a modifier, and a radio button would be an option.



来源:https://stackoverflow.com/questions/49619331/cart-api-v3-cant-create-a-cart-for-product-that-has-options

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!