Skip to content

Unable to add new options to swatch attribute #11032

Closed
@MidnightDesign

Description

@MidnightDesign

ProductAttributeOptionManagementInterface#add() doesn't do anything.

Preconditions

Magento 2.1.9
Create a product attribute named "color" and set its type to "Visual Swatch". Also make sure it has at least one option. Otherwise you'll run into a different issue.

Steps to reproduce

Consider the following class:

<?php declare(strict_types=1);

namespace Acme\AttributeCreator\Model;

use Magento\Catalog\Api\ProductAttributeOptionManagementInterface;
use Magento\Eav\Api\Data\AttributeOptionInterfaceFactory;
use Magento\Eav\Api\Data\AttributeOptionLabelInterfaceFactory;
use Magento\Store\Api\StoreRepositoryInterface;

class AttributeOptionCreator
{
    /** @var AttributeOptionLabelInterfaceFactory */
    private $optionLabelFactory;
    /** @var AttributeOptionInterfaceFactory */
    private $attributeOptionFactory;
    /** @var ProductAttributeOptionManagementInterface */
    private $attributeOptionManagement;
    /** @var StoreRepositoryInterface */
    private $storeRepository;

    public function __construct(
        AttributeOptionLabelInterfaceFactory $optionLabelFactory,
        AttributeOptionInterfaceFactory $attributeOptionFactory,
        ProductAttributeOptionManagementInterface $attributeOptionManagement,
        StoreRepositoryInterface $storeRepository
    ) {
        $this->optionLabelFactory = $optionLabelFactory;
        $this->attributeOptionFactory = $attributeOptionFactory;
        $this->attributeOptionManagement = $attributeOptionManagement;
        $this->storeRepository = $storeRepository;
    }

    public function createAttributeOption(string $attributeCode, string $label): void
    {
        $labels = [];
        foreach ($this->storeRepository->getList() as $store) {
            $optionLabel = $this->optionLabelFactory->create();
            $optionLabel->setStoreId($store->getId());
            $optionLabel->setLabel($label);
            $labels[] = $optionLabel;
        }
        $option = $this->attributeOptionFactory->create();
        $option->setStoreLabels($labels);
        $option->setLabel($label);
        $option->setIsDefault(false);
        $this->attributeOptionManagement->add($attributeCode, $option);
    }
}

Expected result

$attributeOptionCreator->createAttributeOption('color', 'Blue') adds a new option to the attribute.

Actual result

The option is not added to the attribute.

Metadata

Metadata

Assignees

Labels

Fixed in 2.1.xThe issue has been fixed in 2.1 release lineFixed in 2.2.xThe issue has been fixed in 2.2 release lineFixed in 2.3.xThe issue has been fixed in 2.3 release lineIssue: Clear DescriptionGate 2 Passed. Manual verification of the issue description passedIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedIssue: Format is validGate 1 Passed. Automatic verification of issue format passedIssue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for developmentReproduced on 2.1.xThe issue has been reproduced on latest 2.1 releaseReproduced on 2.2.xThe issue has been reproduced on latest 2.2 releaseReproduced on 2.3.xThe issue has been reproduced on latest 2.3 release

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions