How to Create a Configurable Product Using the REST API v2 in Magento2

configurable-productmagento2rest

I am trying to create a configurable product called "Kudos Configurable" with options for the user to select from small green, large green, small orange and large orange.

I have got to the stage where a configurable product has been created via the API using the following JSON in a PUT request to /rest/V1/products/KudosConfig.

    {
      "product":{
        "sku":"KudosConfig",
        "name":"Kudos Configurable",
        "price":30.00,
        "status":4,
        "type_id":"configurable",
        "attribute_set_id":4,
        "weight":1,
        "extension_attributes":{
          "stock_item":{
            "qty":10,
            "is_in_stock":true
           }
         }
       }
     }

The child products have been created using the API using the following JSON for each of 4 items with attributes for size and colour set via the same method.

    {
      "product":{
        "sku":"KudosConfigGreenSmall ",
        "name":"Kudos Configurable Green Small",
        "price":30.00,
        "status":0,
        "type_id":"virtual",
        "visibility":1,
        "attribute_set_id":4,
        "weight":1,
        "extension_attributes":{
          "stock_item":{
            "qty":10,
            "is_in_stock":true
          }
        },
        "custom_attributes":[
          {
            "attribute_code":"kudos_colour",
            "value":"328"
          },
          {
            "attribute_code":"kudos_size",
            "value":"330"
          }
        ]
      }
    }

This is working fine and the products are created with the attributes set correctly.

I had thought the last stage was to use the configurableProductLinkManagementV1 service to link the child products but using a POST request to /rest/V1/configurable-products/KudosConfig/child with the following JSON returns HTTP 400 bad request error.

    {
      "childSku":"KudosConfigGreenSmall"
    }

I am thinking there might be something else I need to do on the parent product but I'm struggling to work out what. Within the catalogProductRepositoryV1 service you can add in an array of configurable_product_options, I've tried playing with this but have only got bad request errors back.

Any light anyone can shed on this would be very much appreciated, already spent way too long on this.

EDIT:

Have now tried adding the configurable_product_options into the configurable product PUT request, this now looks like this.

    {
      "product":{
        "sku":"KudosConfig",
        "name":"Kudos Configurable",
        "price":30.00,
        "status":4,
        "type_id":"configurable",
        "attribute_set_id":4,
        "weight":1,
        "extension_attributes":{
          "stock_item":{
            "qty":10,
            "is_in_stock":true
          },
          "configurable_product_options":[
            {
              "attribute__id":"kudos_colour",
              "label":"Colour",
              "values":[
                {
                  "value_index":340
                },
                {
                  "value_index":341
                }
              ]
            },
            {
              "attribute__id":"kudos_size",
              "label":"Size",
              "values":[
                {
                  "value_index":343
                },
                {
                  "value_index":342
                }
              ]
            }
          ]
        }
      }
    }

When I try this I get a 400 response back with the message:

Something went wrong while saving option.

Not particularly helpful…

Quick bit of Googling suggests that this is the issue https://github.com/magento/magento2/issues/5580.

Next step is to see if the customer is happy to do the hack on the configurable.php file.

EDIT:
Have now tried changing the configurable.php file but doesn't seem to have made a difference, still getting the same error so stumped again.

Best Answer

Finally seem to have this working. I think the issue is that the attribute_id within the configurable product options array should be the numeric id, not the attribute code, like this:

        "configurable_product_options":[
        {
          "attribute__id":"192",
          "label":"Colour",
          "values":[
            {
              "value_index":340
            },
            {
              "value_index":341
            }
          ]
        },
        {
          "attribute__id":"193",
          "label":"Size",
          "values":[
            {
              "value_index":343
            },
            {
              "value_index":342
            }
          ]

Also added the product links in the same request so final configurable product request looks like this:

    {
      "product":{
        "sku":"KudosConfig",
        "name":"Kudos Configurable",
        "price":30,
        "status":1,
        "type_id":"configurable",
        "attribute_set_id":4,
         "extension_attributes":{
           "stock_item":{
             "is_in_stock":true
           },
           "configurable_product_options":[
             {
               "attribute__id":"193",
               "label":"Colour",
               "position":0,
               "values":[
                 {
                   "value_index":340
                 },
                 {
                   "value_index":341
                 }
               ],
               "product_id":299
             },
             {
               "attribute__id":"192",
               "label":"Size",
               "position":1,
               "values":[
                 {
                   "value_index":343
                 },
                 {
                   "value_index":342
                 }
               ],
               "product_id":299
             }
           ],
         "configurable_product_links":[
           300,
           301,
           302,
           303
         ]
       }
     }
   }
Related Topic