Magento2: Impossible to add swatch options via Service Contracts if there is no existing swatch option for attribute

Created on 26 Apr 2017  路  8Comments  路  Source: magento/magento2

Steps to reproduce

  1. Make sure that you have an attribute that is of type "swatches"
  2. Make sure that this attribute does not have any option / swatch
  3. Try to add a swatch option using \Magento\Catalog\Api\ProductAttributeOptionManagementInterface::add

Expected result

The attribute from step 1 above should have a new swatch option.

Actual result

An exception is thrown: "Cannot save attribute [code of the attribute from step 1 above]"
This comes from \Magento\Eav\Model\Entity\Attribute\OptionManagement::add because, on \Magento\Swatches\Model\Plugin\EavAttribute::prepareOptionLinks, $attributeSavedOptions[$optionCounter] is never set ($attributeSavedOptions[1] fails).

Fix

\Magento\Swatches\Model\Plugin\EavAttribute::prepareOptionLinks should be like this:

    protected function prepareOptionLinks(array $optionsArray, array $attributeSavedOptions)
    {
        $dependencyArray = [];
        $optionCounter = 1;
        if (is_array($optionsArray['value']) && isset($attributeSavedOptions[$optionCounter])) {
            foreach (array_keys($optionsArray['value']) as $baseOptionId) {
                $dependencyArray[$baseOptionId] = $attributeSavedOptions[$optionCounter]['value'];
                $optionCounter++;
            }
        }

        $this->dependencyArray = $dependencyArray;
    }

Github extension while waiting for the fix to be implemented

https://github.com/herveguetin/Herve_SwatchOptionFix

Catalog Fixed in 2.1.x Fixed in 2.2.x Fixed in 2.3.x Cannot Reproduce Clear Description Format is not valid needs update bug report

Most helpful comment

Found this issue while trying to add swatches options in an import procedure. I ended up writing direct queries on the involved tables...

Actually all the swatches module is a pile of weird things outside of Magento standards and needs to be extensively refactored.

All 8 comments

Hello! Did you try this solution?

@TomashKhamlai, the solution you are linking does not answer my question. My bug report relates to Service Contracts and does not ask for a solution to actually add swatches. It's just a bug report :)

Moreover, the solution you are providing does not make use of Service Contracts.

Okay, I will answer you when internal ticket will be created. I also tried to test REST API for this functionality and I didn't get what I expected, however, I need some time to dive deeper into this problem. Thank you for reporting.

Found this issue while trying to add swatches options in an import procedure. I ended up writing direct queries on the involved tables...

Actually all the swatches module is a pile of weird things outside of Magento standards and needs to be extensively refactored.

@herveguetin, thank you for your report.
We were not able to reproduce this issue by following the steps you provided. Please provide more detailed steps to reproduce or try to reproduce this issue on a clean installation or latest release.

@herveguetin, we are closing this issue due to inactivity. If you'd like to update it, please reopen the issue.

@magento-engcom-team With "can't reproduce" are you saying that you tried and Magento\Catalog\Api\ProductAttributeOptionManagementInterface::add actually added a swatch option to an attribute?

The issue has been fixed and delivered to 2.2-develop and 2.1-develop branch. Will be available with upcoming patch release

Was this page helpful?
0 / 5 - 0 ratings