magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.49k stars 9.29k forks source link

Rest API create product attribute options - unable to set option value #36939

Open salgua opened 1 year ago

salgua commented 1 year ago

Preconditions and environment

Steps to reproduce

  1. Create an attribute option using this REST API enpoint: POST products/attributes/{attributeCode}/options
  2. Use this payload:
    "option": {
    "label": "aaaaaaa",
    "value": "bbbbb",
    }

Expected result

When I call this REST API endpoint: GET products/attributes/{attributeCode}/options in the result array I expect to obtain an option like this:

{
"label": "aaaaaaa",
"value": "bbbbb"
}

Actual result

when the POST enpoint create the new option, it ignores the value. This is the created option:

{
"label": "aaaaaaa",
"value": "123"
}

where '123' is the progressive option ID.

Additional information

Here is the official enpoint documentation:

https://adobe-commerce.redoc.ly/2.4.5-admin/tag/productsattributesattributeCodeoptions#operation/PostV1ProductsAttributesAttributeCodeOptions

as you can see label and value are mandatory, but value is totally ignored.

Release note

No response

Triage and priority

m2-assistant[bot] commented 1 year ago

Hi @salgua. Thank you for your report. To speed up processing of this issue, make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:

m2-assistant[bot] commented 1 year ago

Hi @engcom-Bravo. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

m2-assistant[bot] commented 1 year ago

Hi @engcom-Lima. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

engcom-Lima commented 1 year ago

Hi @salgua ,

Thanks for your contribution and collaboration.

I have tried to reproduce the issue in fresh 2.4-develop instance and this issue is the expected behaviour of Magento. Whenever options is created for custom attribute multi-select, Magento generate unique increment id for the options created. Below is the screenshot for your reference:

Screenshot 2023-03-01 at 6 28 03 PM Screenshot 2023-03-01 at 5 52 10 PM Screenshot 2023-03-01 at 5 46 54 PM

Let us know if your facing any issue.

Thanks

salgua commented 1 year ago

Hi @engcom-Lima, I suspected it was the default behavior, but at this point I'm wondering: what's the point of passing it the "value" parameter? In your request you passed "value": "m3_value_shu", where does Magento save this data and how do I get it? Thanks

engcom-Lima commented 1 year ago

This ticket needs further Dev triage . Hence, labelling as "Dev Experience"

m2-assistant[bot] commented 1 year ago

Hi @engcom-November. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

engcom-November commented 1 year ago

Hi @salgua , Thank you for the response. Verified the behavior on Magento 2.4-develop instance and is reproducible. Value passed in the payload POST /products/attributes/{attribute-code}/options is being saved / used nowhere in DB tables as well: eav_attribute_option_value, eav_attribute_option, eav_attribute, catalog_product_entity_varchar and catalog_product_entity_int The options are stored in eav_attribute_option_value DB table. Here option_idis the response retrieved from the POST request and value is the label passed in payload and the actual value passed in the payload request is being ignored but as per Magento docs, value parameter can be passed in the request which is existing behavior of Magento but however this is not a mandatory field to be passed in the payload. No behavior change is observed even if value parameter is not passed in the payload request. Hence we can consider this as an enhancement to remove the value parameter in the payload and label as feature request. image image image

Maxi-07 commented 2 months ago

Any updates on this?

I am having the same issue and it is quite vital to synchronize product options from an external system to Magento. Unfortunately, as everything on Adobe/Magento takes years :(

agata-maksymiuk commented 2 weeks ago

@engcom-November @engcom-Hotel

In fact, this value required by this endpoint, is being used for swatch attribute, as swatch attribute option value: eav_attribute_option_swatch.value in database

    private function setSwatchAttributeOption(
        AttributeInterface $attribute,
        AttributeOptionInterface $option,
        string $optionId
    ): void {
        $optionsValue = trim($option->getValue() ?: '');
        if ($this->swatchHelper->isVisualSwatch($attribute)) {
            $attribute->setData('swatchvisual', ['value' => [$optionId => $optionsValue]]);
        } else {
            $options = [];
            $options['value'][$optionId][Store::DEFAULT_STORE_ID] = $optionsValue;
            if (is_array($option->getStoreLabels())) {
                foreach ($option->getStoreLabels() as $label) {
                    if (!isset($options['value'][$optionId][$label->getStoreId()])) {
                        $options['value'][$optionId][$label->getStoreId()] = null;
                    }
                }
            }
            $attribute->setData('swatchtext', $options);
        }
    }

It means it sets this value as swatch text, displayed as on the picture: Shoei-NXR2-MC-Hjälm-Röd