Skip to content

Unable to add new options to swatch attribute #11032

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
MidnightDesign opened this issue Sep 25, 2017 · 9 comments
Closed

Unable to add new options to swatch attribute #11032

MidnightDesign opened this issue Sep 25, 2017 · 9 comments
Assignees
Labels
Fixed in 2.1.x The issue has been fixed in 2.1 release line Fixed in 2.2.x The issue has been fixed in 2.2 release line Fixed in 2.3.x The issue has been fixed in 2.3 release line Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release

Comments

@MidnightDesign
Copy link

MidnightDesign commented Sep 25, 2017

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.

@magento-engcom-team magento-engcom-team added the Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed label Sep 25, 2017
@MidnightDesign
Copy link
Author

This still doesn't work in Magento 2.2.0.

@MidnightDesign
Copy link
Author

I just fount the source of the problem: Magento\Swatches\Model\Plugin\EavAttribute#setProperOptionsArray() (called by beforeBeforeSave()) overrides the option with the optionvisual key.

So it seems to me like options for visual (and text) swatches are supposed to be created differently, but I can't figure out how.

@magento-engcom-team magento-engcom-team added Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed 2.1.x labels Sep 27, 2017
@magento-engcom-team
Copy link
Contributor

@MidnightDesign, thank you for your report.
We've created internal ticket(s) MAGETWO-80165 to track progress on the issue.

@magento-engcom-team magento-engcom-team added Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development develop Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release labels Sep 27, 2017
@springerin
Copy link

I am preparing a PR for this one so if you can assign it to me please #SQUASHTOBERFEST

@LucienC
Copy link

LucienC commented Oct 20, 2017

Same issue here, I didn't dig much into the problem but renaming the function beforeBeforeSave to beforeSave fix the problem.
Here is the commit where the function has been rename if it can help: cafbc8f

@sylvainraye
Copy link
Contributor

I confirm the issue on 2.2.1, it's not possible to add swatch options if the attribute options is not already present/saved into the DB. This happens only when the DB transaction is committed.

However, the method of the plugin \Magento\Swatches\Model\Plugin\EavAttribute::beforeBeforeSave prevent saving the attribute options while saving the attribute. Renaming beforeBeforeSave to beforeSave prevent partially the issue. There is an other issue related to the afterAfterSave and processSwatchOptions methods. The last one need the list of all options but because the DB transaction is not yet committed, the options are not yet available from the database. The method \Magento\Swatches\Model\Plugin\EavAttribute::prepareOptionLinks trigger a notice error because no option id exist YET.

So the solution was to create a di preference of the existing plugin + create a own plugin into a di.xml of a custom module.

SwatchesEavAttribute.php.txt

@okorshenko
Copy link
Contributor

The issue has been fixed and delivered to 2.1-develop and 2.2-develop branches

@okorshenko okorshenko added Fixed in 2.1.x The issue has been fixed in 2.1 release line Fixed in 2.2.x The issue has been fixed in 2.2 release line Fixed in 2.3.x The issue has been fixed in 2.3 release line labels Nov 27, 2017
@frithjof
Copy link

frithjof commented May 2, 2019

it is not fixed in current 2.3.1!
$option = $this->optionDataFactory->create();
$option->setLabel($label);

		$result = $this->attributeOptionManagementInterface->add($attributeCode, $option);

fails with a lock wait timeout on swatch attributes

fix from sylvainraye works

@AndreyRozumny
Copy link

AndreyRozumny commented Jun 4, 2020

Faced the same issue 2.3.5-p1
Basically options are created but not added to the swathes table. But it seems like it works if you set option Value, the same as option label and it's finally added into my attribute.
So in my case it's something like:

$optionLabel = $this->optionLabelFactory->create();
            $optionLabel->setStoreId(0);
            $optionLabel->setLabel($label);

            $option = $this->optionFactory->create();
            $option->setLabel($label);
            $option->setValue($label);
            $option->setStoreLabels([$optionLabel]);
            $option->setSortOrder(0);
            $option->setIsDefault(false);

            $this->attributeOptionManagement->add(
                $this->getAttribute($attributeCode)->getAttributeId(),
                $option
            );

Not sure is it correct, but this seems to be work solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fixed in 2.1.x The issue has been fixed in 2.1 release line Fixed in 2.2.x The issue has been fixed in 2.2 release line Fixed in 2.3.x The issue has been fixed in 2.3 release line Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release
Projects
None yet
Development

No branches or pull requests

8 participants