Skip to content

Cannot add multiple options to configurable product via REST API #5580

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
svines opened this issue Jul 11, 2016 · 20 comments
Closed

Cannot add multiple options to configurable product via REST API #5580

svines opened this issue Jul 11, 2016 · 20 comments
Assignees
Labels
bug report Event: distributed-cd Distributed Contribution Day Fixed in 2.3.x The issue has been fixed in 2.3 release line Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is not valid Gate 1 Failed. Automatic verification of issue format is failed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development non-issue Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release

Comments

@svines
Copy link

svines commented Jul 11, 2016

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.

@svines
Copy link
Author

svines commented Jul 11, 2016

I also notice that the associated unit test only tests the creation of a single option, so it would miss this.

@CodeGlitcher
Copy link

CodeGlitcher commented Jul 12, 2016

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

@svines
Copy link
Author

svines commented Jul 12, 2016

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.

@CodeGlitcher
Copy link

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: #5243

@svines
Copy link
Author

svines commented Jul 12, 2016

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").

@svines
Copy link
Author

svines commented Jul 14, 2016

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?

@aminy
Copy link

aminy commented Nov 11, 2016

Is there any fix for this bug yet? (Other than hacking the configurable.php)

@ericahoekerd
Copy link

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?

@Samd252
Copy link

Samd252 commented Jan 26, 2017

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.

@CatherineLearmonth
Copy link

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
}
]
}

@Samd252
Copy link

Samd252 commented Jan 26, 2017

Thanks for that, it didn't work for me. I get told that the property 'label' doesn't have a setter.

@Samd252
Copy link

Samd252 commented Jan 26, 2017

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.

@CatherineLearmonth
Copy link

CatherineLearmonth commented Jan 26, 2017

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.

@smoskaluk
Copy link

Internal ticket created MAGETWO-70524

@veloraven veloraven added the Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development label Jul 7, 2017
@magento-engcom-team magento-engcom-team added bug report Issue: Format is not valid Gate 1 Failed. Automatic verification of issue format is failed labels Sep 11, 2017
@magento-engcom-team magento-engcom-team added the Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed label Oct 17, 2017
@magento-engcom-team
Copy link
Contributor

@svines, thank you for your report.
We've created internal ticket(s) MAGETWO-70524 to track progress on the issue.

@magento-engcom-team magento-engcom-team added 2.1.x Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release labels Oct 17, 2017
@bcerban
Copy link
Contributor

bcerban commented Mar 24, 2018

I'd like to work on this! #distributed-cd

@magento-engcom-team
Copy link
Contributor

@bcerban thank you for joining. Please accept team invitation here and self-assign the issue.

@bcerban bcerban self-assigned this Mar 24, 2018
@bcerban
Copy link
Contributor

bcerban commented Mar 24, 2018

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
      }
    ]
  }
}

@okorshenko
Copy link
Contributor

Hi @svines we can closing the ticket as non-issue. @bcerban thank you for research and provided API functional test 👍

@okorshenko okorshenko added the Fixed in 2.3.x The issue has been fixed in 2.3 release line label Mar 27, 2018
@okorshenko
Copy link
Contributor

Hi @svines. Thank you for your report.
The issue has been fixed in #14345 by @bcerban in 2.3-develop branch
Related commit(s):

The fix will be available with the upcoming 2.3.0 release.

magento-engcom-team pushed a commit that referenced this issue Apr 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug report Event: distributed-cd Distributed Contribution Day Fixed in 2.3.x The issue has been fixed in 2.3 release line Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is not valid Gate 1 Failed. Automatic verification of issue format is failed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development non-issue Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release
Projects
None yet
Development

No branches or pull requests