8

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.

6

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
         ]
       }
     }
   }
  • Why did you add "product_id":299 ? – DevonDahon Jun 30 '17 at 2:40
  • I'm not sure, it wasn't necessary in the end and it's not in the final code. It was probably something I was trying to get it working – SD252 Jul 1 '17 at 11:31
  • I realized it was actually added automatically when creating the configurable product. – DevonDahon Jul 1 '17 at 16:45
  • sir, can i use this code in controller ? for add configurable product ? if yes , then tel me how to save ? – Nikhil waghela Aug 29 '17 at 10:25
  • Yes feel free to use it, I'm not sure what you mean about how to save it. – SD252 Aug 30 '17 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.