Skip to content

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

Closed
@herveguetin

Description

@herveguetin

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

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions