Skip to content

2.2 develop 11032 Unable to add new options to swatch attribute #11628

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

Conversation

springerin
Copy link

ProductAttributeOptionManagementInterface#add() doesn't do anything.

Description

On the beforeBeforeSave(Attribute $attribute) in the case of a swatch attribute, expects an array with key swatchtext / swatchvisual ( first in case of textswatch, second in case of visualswatch) that overwrites the option array originally given, so if this is empty, it will be emptied as well. So I added a plugin that right before doing this, creates the expected array that will be used later for the creation of the actual option.

Fixed Issues (if relevant)

  1. Unable to add new options to swatch attribute #11032: Unable to add new options to swatch attribute
  2. Impossible to add swatch options via Service Contracts if there is no existing swatch option for attribute #9410: Impossible to add swatch options via Service Contracts if there is no existing swatch option for attribute

Manual testing scenarios

  1. Call the rest api http:///index.php/rest/V1/products/attributes/color/options to add a new color option with the following input
    { "option": { "label": "new color", "value": "", "sort_order": 0, "is_default": true, "store_labels": [ { "store_id": 0, "label": "new color" }, { "store_id": 1, "label": "new color" } ] } }
  2. Check from the admin area that your new option is there or call the rest api http:///index.php/rest/V1/products/attributes/color/options

Contribution checklist

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds on Travis CI are green)

@springerin
Copy link
Author

gonna fix what failed this weekend.

@dmanners dmanners self-assigned this Oct 26, 2017
@dmanners dmanners added this to the October 2017 milestone Oct 27, 2017
@dmanners dmanners added Release Line: 2.2 2.2.x bug report Component: Catalog 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 Oct 27, 2017
Copy link
Contributor

@dmanners dmanners left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a couple of comments that should be addressed, also I am still not 100% sure about this PR if it is the "correct" way to fix this bug.

/**
* @var Config
*/
protected $eavConfig;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we have a new class here the default is to stay away from protected in properties and functions. Recommended is private unless the method is needed outside the class.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

SwatchHelper $swatchHelper,
OptionCollection $attrOptionCollectionFactory
) {
$this->eavConfig = $eavConfig;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spacing looks a bit off here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

$optionKey = self::PREFIX_OPTION . $isSwatch;
$swatchKey = self::PREFIX_SWATCH . $isSwatch;

if (!empty($isSwatch) && $attribute->getData($optionKey) === null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you maybe update this to look as follows for readability:

if (
    !empty($isSwatch)
    && $attribute->getData($optionKey) === null
    && $attribute->getData($swatchKey) === null
) {

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

{
$options = $this->getOptionsByAttributeIdWithSortOrder($attributeId);
$attributeData = $this->getOptionsForSwatch($options, $optionKey);
return $attributeData;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could just return $this->getOptionsForSwatch($options, $optionKey); here rather than set $attributeData.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

protected function getStoreLabels($optionId)
{
$optionCollection = $this->optionCollection->create();
$connection = $optionCollection->getConnection();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again spacing seems a bit off

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

) {
$this->eavConfig = $eavConfig;
$this->swatchHelper = $swatchHelper;
$this->optionCollection = $attrOptionCollectionFactory;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically at this point this is the factory still right? Might be good to keep that obvious in the naming of the property.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

*
* @return null|string
*/
protected function isSwatch($attribute)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The naming of this method is a bit confusing to me. is would suggest to me that it will return a boolean but it actually returns a mix of string and null. I think it might be a bit more readable if there was two methods, 1 for returning a boolean and one for getting the swatch type or something like that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

*
* @return array
*/
protected function getStoreLabels($optionId)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Part of me thinks that both this method and getOptionsByAttributeIdWithSortOrder should not be in the plugin but in another class.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed for getOptionsByAttributeWithSortOrder but still to be fixed for storeLabels

*
* @return array
*/
protected function getOptionsForSwatch($options, $optionKey)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can make sure an array is passed in here with array $options if it is always going to be an array.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@dmanners
Copy link
Contributor

@springerin the following API test is failing. dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductAttributeOptionManagementInterfaceTest.php could you have a quick look into this failure for me?

@springerin
Copy link
Author

sure I will check it out today and let you know.

@springerin
Copy link
Author

locally tested with the last updates and still green for me. Waiting for more info to reproduce the error.

@magento-engcom-team magento-engcom-team 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 30, 2017
@dmanners
Copy link
Contributor

@springerin it seems that this has now be fixed by #12036. Using the 2.2-develop branch I followed your test case and now the swatch options have been added.

@dmanners dmanners closed this Dec 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug report bugfix Component: Catalog 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: Cannot Reproduce Cannot reproduce the issue on the latest `2.4-develop` branch Progress: needs update Release Line: 2.2 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

Successfully merging this pull request may close these issues.

5 participants