Search code examples
commercetoolssphere.io

Sphere.IO : How can we set a custom attribute during product creation?


After we have created a productType and have its type id, how can we set custom attributes during product create API calling. Until now what I've been doing is 1st, create the product, then update the custom attribute. But, I am unable to do so with the required attributes.


Solution

  • you can supply all the custom attributes in the product creation payload. Example - here I provide values for attributes brand on the master variant, for a previously created product type which specifies that there is an attribute called Brand:

      {
      "productType": {
        "typeId": "product-type",
        "id": "6c06998a-c576-4a8d-8ace-dc306d70e1d1"
      },
      "name": {
        "en": "Some Product"
      },
      "slug": {
        "en": "product_slug_sdfsdfsdfdsdfsdfdssdf"
      },
      "masterVariant": {
        "prices": [{
          "value": {
            "centAmount": 2500,
            "currencyCode": "EUR"
          },
          "country": "US"
        }],
        "attributes": [{
          "name": "brand",
          "value": "Hugo Boss"
        }]
      },
      "variants": [{
        "prices": [{
          "value": {
            "centAmount": 2600,
            "currencyCode": "EUR"
          },
          "country": "US"
        }],
        "attributes": [{
          "name": "brand",
          "value": "Hugo Boss"
        }]
      }]
    }