2

I actually have to add some custom fields to every line item within the commercetools platform.

Line Item Docs => http://dev.sphere.io/http-api-projects-carts.html#line-item

There I found this: => http://dev.sphere.io/http-api-projects-custom-fields.html#custom-fields

But apparently the docs for custom-fields are way too less in terms of showing "how to use them". Does somebody has any experience with that? A json example would be wonderful, with a bit more explanation. Thanks in advance.

Emtii
  • 21
  • 3
  • by the way we also got this new FAQ here on that topic that is hopefully proving helpful: http://dev.sphere.io/dev/tutorial-custom-types.html – sebbulon Oct 19 '15 at 14:07

1 Answers1

2

you can create a custom type for line items using the resource type ID "line-item" or "custom-line-item" (http://dev.sphere.io/http-api-projects-custom-fields.html#customizable-resource ) - example:

 {
  "key": "myLineItemType",
  "name": { "en": "my line item type" },
  "resourceTypeIds": ["line-item"],
  "fieldDefinitions": [
    {
      "type":{
          "name":"LocalizedString"
      },
      "name":"myField",
      "label":{
        "en":"my field",
        "de":"mein feld"
      },
      "required":false,
      "inputHint":"SingleLine"
    }
  ]
}

Then there are 2 ways of using the new custom type and the new field.

  1. You can set the custom type and a value at the time you create a line item using the "addLineItem" Update action on the cart resource - see this JSON example for instance:

    {
      "version": 19,
      "actions": [{
        "action": "addLineItem",
        "productId": "9f19f37d-ec10-4ccf-9ff8-e5a295de0c3e",
        "variantId": 1,
        "quantity": 1
      }],
      "custom": {
            "typeKey": "myLineItemType",
            "fields": {
              "myField": {
                "en":"whats up",
                "de":"was ist los"
              }
            }
         }
    }
    
  2. You can set the custom type of the line item with the "setLineItemCustomType" update action on the cart to make the field available. This can work with existing line items.

http://dev.sphere.io/http-api-projects-carts.html#set-line-item-custom-type

sebbulon
  • 613
  • 7
  • 21