magento2: Cannot add multiple options to configurable product via REST API

Steps to reproduce

  1. Create a new configurable product
  2. Use the REST API to add a new option to the new product (this works): URL: http://[base url]/rest/V1/configurable-products/basecap/options POST BODY: { “option” : { “attribute_id”: “font_type”, “label”: “font type”, “position”: 0, “values”: [ { “value_index”: 216 } ] } }
  3. Use the REST API to add a second option to the new product: URL: http://[base url]/rest/V1/configurable-products/basecap/options POST BODY: { “option” : { “attribute_id”: “font_color”, “label”: “font color”, “position”: 1, “values”: [ { “value_index”: 213 }, { “value_index”: 214 } ] } }

The second option returns error “Something went wrong while saving option.”

Expected result

  1. Additional option added to configurable product and a 200 response from REST API.

Actual result

{ “message”: “Something went wrong while saving option.”, “trace”: “#0 [internal function]: Magento\ConfigurableProduct\Model\OptionRepository->save(‘basecap’, Object(Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute))\n#1 /var/www/magento2-dev/vendor/magento/module-webapi/Controller/Rest.php(265): call_user_func_array(Array, Array)\n#2 /var/www/magento2-dev/vendor/magento/module-webapi/Controller/Rest.php(160): Magento\Webapi\Controller\Rest->processApiRequest()\n#3 /var/www/magento2-dev/var/generation/Magento/Webapi/Controller/Rest/Interceptor.php(24): Magento\Webapi\Controller\Rest->dispatch(Object(Magento\Framework\App\Request\Http))\n#4 /var/www/magento2-dev/vendor/magento/framework/App/Http.php(115): Magento\Webapi\Controller\Rest\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))\n#5 /var/www/magento2-dev/vendor/magento/framework/App/Bootstrap.php(258): Magento\Framework\App\Http->launch()\n#6 /var/www/magento2-dev/pub/index.php(37): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http))\n#7 {main}” }

Additional observation: when enabling mysql general logging, I see that something deletes the attribute immediately after creating it:

(mysql log entry): DELETE FROM catalog_product_super_attribute WHERE (product_super_attribute_id=‘426’)

This occurs immediately before the code checks to verify that the attribute created successfully on line 238 of vendor/magento/module-configurable-product/Model/OptionRepository.php in v2.0.7: if (!$configurableAttribute->getId()) { throw new CouldNotSaveException(__(‘Something went wrong while saving option.’)); }

This issue occurs in both 2.0.7 and 2.1.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 20 (6 by maintainers)

Commits related to this issue

Most upvoted comments

After much pain and suffering, I have pinpointed it to a single line of code in v2.0.7: File: https://github.com/magento/magento2/blob/2.0.7/app/code/Magento/ConfigurableProduct/Model/Product/Type/Configurable.php Line: 574 Code: $configurableAttributesCollection->walk(‘delete’);

Commenting this line fixes this issue, but I don’t know what others it might cause. Why was this line there?

The problem here is in the request body data: in the examples, you are passing the attribute code as the attribute_id (an understandable mistake, since the interface accepts a string rather than an int value).

If you pass the attribute code, an entry in table catalog_product_super_attribute is saved, with attribute_id = 0. After that, it is impossible to load the product in \Magento\ConfigurableProduct\Model\Product\ReadHandler::execute(). The failure happens when trying to load the options collection via $this->optionLoader->load($entity).

The fix then, is to pass the attribute ID, so that you request body would be something like:

{
  "option": {
    "attribute_id": "93",
    "label": "Color",
    "position": 0,
    "values": [
      {
        "value_index": 49
      }
    ]
  }
}