magento2: Cannot add multiple options to configurable product via REST API
Steps to reproduce
- Create a new configurable product
- 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 } ] } }
- 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
- 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
- Merge pull request #5580 from magento-tsg-csl3/2.4-develop-pr22 [TSG-CSL3] For 2.4 (pr22) — committed to magento/magento2 by viktym 4 years ago
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_attributeis saved, withattribute_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: