Skip to content

Can't import attribute option over API if option is 'visual swatch' #10737

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
slavkodick opened this issue Sep 1, 2017 · 22 comments
Closed

Can't import attribute option over API if option is 'visual swatch' #10737

slavkodick opened this issue Sep 1, 2017 · 22 comments
Assignees
Labels
Component: Framework/Webapi USE ONLY for FRAMEWORK RELATED BUG! E.g If bug related to Catalog WEB API use just Catalog Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Progress: needs update

Comments

@slavkodick
Copy link

Preconditions

  1. Magento 2.1.8

Steps to reproduce

  1. Import Attribute Option of type visual swatch (Color for example) over http request (API)

  2. Code example:
    {
    $option = $this->attrOptionFactory->create();
    $optionLabel = $this->attrOptionLabelFactory->create();

     $optionLabel->setStoreId(0);
     $optionLabel->setLabel('new_color');
    
     $option->setLabel('new_color');
     $option->setStoreLabels(array($optionLabel));
     
     $this->productAttributeOptionManagement->add('color', $option);
    

    }

Expected result

  1. New Attribute Option should be imported to backend

Actual result

  1. New Atttribute Option does not get imported because there is no entry in eav_attribute_option_swatch table

More info

Until Magento 2.1.7 the plugin did not trigger beforeSave function in swatches/Model/Plugin/EavAttribute and the Import data had not to match visual swatch data structure, it was imported
Since Magento 2.1.8 the function is renamed to beforeBeforeSave and the plugin triggers it,
so the Import data has to follow data structure of visual swatch, but since it is a API Import there is no entry in database and the new Option does not get imported

@misha-kotov misha-kotov added 2.1.x bug report Component: Framework/Webapi USE ONLY for FRAMEWORK RELATED BUG! E.g If bug related to Catalog WEB API use just Catalog labels Sep 1, 2017
@magento-engcom-team magento-engcom-team added 2.1.x bug report Component: Framework/Webapi USE ONLY for FRAMEWORK RELATED BUG! E.g If bug related to Catalog WEB API use just Catalog labels Sep 5, 2017
@magento-engcom-team magento-engcom-team added Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed 2.1.x bug report Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Component: Framework/Webapi USE ONLY for FRAMEWORK RELATED BUG! E.g If bug related to Catalog WEB API use just Catalog and removed G1 Passed labels Sep 11, 2017
@magento-engcom-team
Copy link
Contributor

We are closing this issue because we cannot reproduce this issue as described. If you have questions, please refer to a question and answer site, such as Stack Exchange and the Magento Forums, where Magento community members can quickly provide recommendations and advice.

@magento-engcom-team magento-engcom-team added Issue: Cannot Reproduce Cannot reproduce the issue on the latest `2.4-develop` branch and removed bug report labels Sep 19, 2017
@slavkodick
Copy link
Author

It seems that's the same problem as #10707, could you leave the issue open until it's solved?

@orlangur
Copy link
Contributor

@slavkodick why keep two issues for the same problem open?

@josephmcdermott
Copy link
Contributor

josephmcdermott commented Feb 19, 2018

What is the definition of 'fixed' here? With the latest Swatch module from 2.2-develop we can import a new Swatch Option via the API method, however the required Admin value is empty: Magento\Catalog\Api\ProductAttributeOptionManagementInterface::add

I do not see anywhere in the \Magento\Eav\Api\Data\AttributeOptionInterface to append Swatch information?

For example:

{
    "option": {
        "label": "API Test Label",
        "value": "API Test Value",
        "sort_order": 0,
        "is_default": false,
        "store_labels": [
            {
                "store_id": 0,
                "label": "API Test Label Store 0"
            },
            {
                "store_id": 1,
                "label": "API Test Label Store 1"
            }
        ]
    }
}

Results in a half-populated Option:
incomplete-swatch-option

Am I missing something?

@durgagupta
Copy link

durgagupta commented Mar 12, 2018

i am also facing same issue could not find node to add hex code or image

url: http://{domain_name}/index.php/rest/V1/products/attributes/{attribute_code}/options
Method: POST

{
  "option": {
        "label": "default store label",
        "value": "46477",
        "sort_order": 0,
        "is_default": false,
        "store_labels": [
          {
            "store_id": 1,
            "label": "en store label"
          },
          {
            "store_id": 2,
            "label": "NL store label"
          },
          {
            "store_id": 3,
            "label": "DE store label"
          },
          {
            "store_id": 4,
            "label": "FR store label"
          }
        ]
      }
}

@orlangur
Copy link
Contributor

Reopening for recheck by @magento-engcom-team.

@orlangur orlangur reopened this Mar 13, 2018
@orlangur orlangur removed the Issue: Cannot Reproduce Cannot reproduce the issue on the latest `2.4-develop` branch label Mar 13, 2018
@orlangur orlangur removed the Fixed in 2.3.x The issue has been fixed in 2.3 release line label Mar 13, 2018
@magento-engcom-team
Copy link
Contributor

@slavkodick, thank you for your report.
We were not able to reproduce this issue by following the steps you provided. If you'd like to update it, please reopen the issue.

@magento-engcom-team magento-engcom-team added the Issue: Cannot Reproduce Cannot reproduce the issue on the latest `2.4-develop` branch label Mar 13, 2018
@EBotha1
Copy link

EBotha1 commented Jul 4, 2018

@magento-admin @magento-devops @magento-engcom-team I am facing the same issue. In Postman we are sending a soap request to create a new 'option' for a text/swatch attribute.
Post Body:
<?xml version="1.0" encoding="UTF-8"?> <soap:Envelope xmlns:def="XXX/soap/all?services=catalogProductAttributeOptionManagementV1" xmlns:soap="http://www.w3.org/2003/05/soap-envelope"> <soap:Body> <def:catalogProductAttributeOptionManagementV1AddRequest> <attributeCode>d_textswatch</attributeCode> <option> <sortOrder>0</sortOrder> <isDefault>false</isDefault> <label>HelloE472</label> <value/> <store_labels> <store_id>0</store_id> <label>HelloE472_0</label> </store_labels> </option> </def:catalogProductAttributeOptionManagementV1AddRequest> </soap:Body> </soap:Envelope>

With Magento 2.2.4 I am getting the error "Cannot save attribute %1". With Magento 2.2.0 I am getting response "true" but the option is not being created in Magento. See here

@mhenkel1
Copy link

mhenkel1 commented Mar 5, 2019

@magento-engcom-team Since you are unable to reproduce, please explain how you add an option with a hex code to an swatch attribute by using the rest api. YOU WILL NOT BE ABLE TO DO THIS, thats how you reproduce it. Next time please read the issue fully.

@shayzalman
Copy link

This is completely ridiculous!!
If the REST API is not supporting the creation of Visual Swatches with swatch options, you should write it down here!

@harperruss
Copy link

harperruss commented Jan 30, 2020

This has been reproduced on a vanilla 2.3.3 installation running the standard demo data/Luma theme:

Steps to reproduce:

  1. Create a new product attribute with a type of Visual Swatch
  2. POST a payload to rest/V1/products/attributes/{attributecode}/options

Example Payload

{
    "option": {
        "label": "Red",
        "is_default": "false",
        "sort_order": 0,
        "store_labels": [
            {
                "store_id": 1,
                "label": "Red"
            }
        ]
    }
}
  1. Check the contents of eav_attribute_option_swatch and observe a record is missing for the new attribute value
  2. Open the attribute in the admin and save which adds a record to eav_attribute_option_swatch

@diazwatson diazwatson reopened this Jan 30, 2020
@diazwatson diazwatson removed the Issue: Cannot Reproduce Cannot reproduce the issue on the latest `2.4-develop` branch label Jan 30, 2020
@engcom-Delta engcom-Delta self-assigned this Feb 3, 2020
@m2-assistant
Copy link

m2-assistant bot commented Feb 3, 2020

Hi @engcom-Delta. 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: 👇

  • 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).

    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.

  • 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • 4. Verify that the issue is reproducible on 2.4-develop branch

    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

  • 5. Add label Issue: Confirmed once verification is complete.

  • 6. Make sure that automatic system confirms that report has been added to the backlog.

@duongnvk55
Copy link

Hi guys,
I tested this ticket on Magento Fresh 2.3 and found the problem of the issue.
My solution: after saved option to attribute, we will need to get option_id and add to swatch data, like this :

  1. Function add option to attribute
public function add($entityType, $attributeCode, $option)
    {
        if (empty($attributeCode)) {
            throw new InputException(__('The attribute code is empty. Enter the code and try again.'));
        }

        $attribute = $this->attributeRepository->get($entityType, $attributeCode);
        if (!$attribute->usesSource()) {
            throw new StateException(__('The "%1" attribute doesn\'t work with options.', $attributeCode));
        }

        $optionLabel = $option->getLabel();
        $optionId = $this->getOptionId($option);
        $options = [];
        $options['value'][$optionId][0] = $optionLabel;
        $options['order'][$optionId] = $option->getSortOrder();

        if (is_array($option->getStoreLabels())) {
            foreach ($option->getStoreLabels() as $label) {
                $options['value'][$optionId][$label->getStoreId()] = $label->getLabel();
            }
        }

        if ($option->getIsDefault()) {
            $attribute->setDefault([$optionId]);
        }

        $attribute->setOption($options);
        try {
            $this->resourceModel->save($attribute);
            if ($optionLabel && $attribute->getAttributeCode()) {
                $this->setOptionValue($option, $attribute, $optionLabel);
                if($this->swatchHelper->isSwatchAttribute($attribute)) {
                    $this->saveSwatchValue($option, $attribute, $optionLabel);
                }
            }
        } catch (\Exception $e) {
            throw new StateException(__('The "%1" attribute can\'t be saved.', $attributeCode));
        }

        return $this->getOptionId($option);
    }
  1. function save option_id to swatch
private function saveSwatchValue(
        \Magento\Eav\Api\Data\AttributeOptionInterface $option,
        \Magento\Eav\Api\Data\AttributeInterface $attribute,
        string $optionLabel
    ){
        $optionId = $attribute->getSource()->getOptionId($optionLabel);
        if ($optionId) {
            $swatch['value'] = [$optionId => ''];
            $attribute->unsOption();
            $attribute->setSwatch($swatch);
            $attribute->save();
        } elseif (is_array($option->getStoreLabels())) {
            foreach ($option->getStoreLabels() as $label) {
                if ($optionId = $attribute->getSource()->getOptionId($label->getLabel())) {
                    $swatch['value'] = [$optionId => ''];
                    $attribute->unsOption();
                    $attribute->setSwatch($swatch);
                    $attribute->save();
                    break;
                }
            }
        }
    }

@engcom-Delta
Copy link
Contributor

Hi @harperruss, thank you for your report. Unfortunately, I am not able to reproduce issue by steps you described on clean 2.4-develop.

Manual testing scenario:

  • Create a new product attribute with a type of Visual Swatch.
    #10737issueSwathAdmin
    #10737issueSwatchDB
  • POST a payload to rest/V1/products/attributes/{attributecode}/options
{
    "option": {
        "label": "red",
        "value": "#ff0000",
        "is_default": "false",
        "sort_order": 0,
        "store_labels": [
            {
                "store_id": 1,
                "label": "red default"
            }
        ]
    }
}

Result:
✔️ Color is added to Swatch on attribute edit page
#10737issueResultAdmin
#10737issueResultDB

Are you still facing this issue on the latest 2.4-develop branch?

@harperruss
Copy link

harperruss commented Feb 3, 2020

@engcom-Delta If you don't send the "value" in the payload or try null or "" it will not create a record in eav_attribute_option_swatch.

We are using visual swatches but the image is coming from the swatch image attribute attached to the product not as a hex colour defined against the attribute:

{
    "option": {
        "label": "red",
        "is_default": "false",
        "sort_order": 0,
        "store_labels": [
            {
                "store_id": 1,
                "label": "red default"
            }
        ]
    }
}

Or

{
    "option": {
        "label": "red",
        "value": "",
        "is_default": "false",
        "sort_order": 0,
        "store_labels": [
            {
                "store_id": 1,
                "label": "red default"
            }
        ]
    }
}

Or

{
    "option": {
        "label": "red",
        "value": null,
        "is_default": "false",
        "sort_order": 0,
        "store_labels": [
            {
                "store_id": 1,
                "label": "red default"
            }
        ]
    }
}

This is on 2.3.3 not 2.4

@diazwatson
Copy link
Contributor

@harperruss could reproduce the issue in 2.4-develop branch?

@harperruss
Copy link

@diazwatson
Copy link
Contributor

@harperruss it looks like, although it does not mention the commit where this was fixed, so we cannot see what was the actual fix. Is it possible for you to test in 2.3.4?

@vincentteyssier
Copy link

@harperruss how do you attach the Swatch image to the option?
Do you need to pass the image path in value or the base64 image encoding?
I only manage to attach a color via the HEX value.

@harperruss
Copy link

@vincentteyssier No idea I'm afraid, it's not something I have needed to do

@vincentteyssier
Copy link

@harperruss if only they would have proper doc...

@diazwatson
Copy link
Contributor

Guys I am closing this issue as it seems to be fixed.
Please feel free to re open if you thing is needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Framework/Webapi USE ONLY for FRAMEWORK RELATED BUG! E.g If bug related to Catalog WEB API use just Catalog Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Progress: needs update
Projects
None yet
Development

No branches or pull requests