The second option returns error "Something went wrong while saving option."
{
"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.
I also notice that the associated unit test only tests the creation of a single option, so it would miss this.
I think you are trying to add the same attribute twice to a product.
{"option":{"attribute_id":"194","label":"color2","position":0,"is_use_default":true,"values":[{"value_index":91},{"value_index":92},{"value_index":93}]}}
i only get an error when executing this request twice. I think you need to look ad updating an existing option.
Also it looks like that the values are only saved if there are child products with that value connected to the parent.
edit
I only tested this at magento 2.1
I was adding "font type" and "font color." I did not try associating the children with the parent before creating the parent options, though, so I'll give that a shot and report back.
1 thing you may run into is that you cannot add a child to a product with:
/V1/configurable-products/:sku/child on this moment
see: https://github.com/magento/magento2/issues/5243
No dice. This time I associated the children first and ran the steps above, but I still get the same error when adding the second option to the parent. Also, if I had been adding a duplicate option it would have been a different error ("product already has this option").
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?
Is there any fix for this bug yet? (Other than hacking the configurable.php)
Same issue on Magento 2.1.2 with SOAP API, the error message 'Something went wrong while saving option' is quite non-informative.
When will the fix provided or when will there be clear response?
I'm also having the same issue in 2.1.3. I've tried changing the configurable.php as well but still getting the same issue.
I have been able to add a second option through the api. I noticed that some of the values I have used to add the option are missing in your request. Try adding the 'code' value in the option, and the label and attribute id values in the values array - you'd need to get the actual values from the eav_attribute table. For example -
"option":
{
'attribute_id' => "font_color",
'code' => eav attribute code,
'label' => font color
'position' => 1,
'values': [
{
'label' => eav label,
'attribute_id' => eav Id,
'value_index' => 214
}
]
}
Thanks for that, it didn't work for me. I get told that the property 'label' doesn't have a setter.
Sorry, that's not quite right. I get the 'Something went wrong while saving option' when using the catalogProductRepositoryV1 service. When using the configurableProductOptionRepositoryV1 service the first attribute returns a 200 response but the second returns a 400 response with message '{"message":"Invalid entity_type specified: %1","parameters":[null]}'. Interestingly after this if I try to open the configurable product in the admin interface it gives the same message and doesn't show any of the attributes.
I confess I am calling the api differently - in the same way as this example here:
dev/tests/integration/testsuite/Magento/configurableproduct/_files/product_configurable.php
So once I have built the configurableAttributesData then I set them as configurableOptions extension attributes on the product as below:
$optionsFactory = $this->objectManager->create(Factory::class);
$configurableOptions = $optionsFactory->create($configurableAttributesData);
$extensionConfigurableAttributes = $magentoProduct->getExtensionAttributes();
$extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
$extensionConfigurableAttributes->setConfigurableProductLinks($optionIds);
$magentoProduct->setExtensionAttributes($extensionConfigurableAttributes);
I don't know how the REST api compares to the above, but thought it would be calling the same code under the hood. I'm not sure how to map what I've done with what you need to do unfortunately.
Internal ticket created MAGETWO-70524
@svines, thank you for your report.
We've created internal ticket(s) MAGETWO-70524 to track progress on the issue.
I'd like to work on this! #distributed-cd
@bcerban thank you for joining. Please accept team invitation here and self-assign the issue.
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
}
]
}
}
Hi @svines we can closing the ticket as non-issue. @bcerban thank you for research and provided API functional test 馃憤
Hi @svines. Thank you for your report.
The issue has been fixed in magento/magento2#14345 by @bcerban in 2.3-develop branch
Related commit(s):
The fix will be available with the upcoming 2.3.0 release.
Most helpful comment
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?