Emtii
Emtii

Reputation: 21

how to use custom fields in line items within commercetools platform

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.

Upvotes: 2

Views: 2021

Answers (2)

srilatha
srilatha

Reputation: 38

First you need to create type using lineitem. Please follow below json to create type. {

        "key": "apLineItemCustomFields",
        "name": {
            "en": "Line Item"
        },
        "description": {
            "en": " when custom fields or actions ."
        },
        "resourceTypeIds": [
            "line-item"
        ],
        "fieldDefinitions": [
            {
                "name": "order-status",
                "label": {
                    "en": "order-status"
                },
                "required": false,
                "type": {
                    "name": "String"
                },
                "inputHint": "SingleLine"
            },
            {
                "name": "productLanguage",
                "label": {
                    "en": "Product Language"
                },
                "required": false,
                "type": {
                    "name": "String"
                },
                "inputHint": "SingleLine"
            }
        ]
    }

Using above json it will create 2 custom fields in lineitem(String type).

Use set custom field in cart API {

"actions": [
    {
        "action" : "setCustomField",
        "name" : "productLanguage",
        "value" : "English"
      }
]

}

you need to fill custom field value using this json.

Upvotes: 1

sebbulon
sebbulon

Reputation: 623

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

Upvotes: 2

Related Questions