From e805fcb16a5f36eea59019dd6cc8feaec00d0a04 Mon Sep 17 00:00:00 2001 From: andra lungu Date: Sun, 15 Oct 2017 17:09:31 +0200 Subject: [PATCH 1/6] fix for https://github.com/magento/magento2/issues/11032 - Unable to add new options to swatch attribute --- .../Catalog/AddSwatchDataToAddOption.php | 171 ++++++++++++++++++ app/code/Magento/Swatches/etc/di.xml | 3 + ...AttributeOptionManagementInterfaceTest.php | 102 +++++++++++ 3 files changed, 276 insertions(+) create mode 100644 app/code/Magento/Swatches/Plugin/Catalog/AddSwatchDataToAddOption.php create mode 100644 dev/tests/api-functional/testsuite/Magento/Swatches/Api/ProductAttributeOptionManagementInterfaceTest.php diff --git a/app/code/Magento/Swatches/Plugin/Catalog/AddSwatchDataToAddOption.php b/app/code/Magento/Swatches/Plugin/Catalog/AddSwatchDataToAddOption.php new file mode 100644 index 0000000000000..51b661b45bb36 --- /dev/null +++ b/app/code/Magento/Swatches/Plugin/Catalog/AddSwatchDataToAddOption.php @@ -0,0 +1,171 @@ +eavConfig = $eavConfig; + $this->swatchHelper = $swatchHelper; + $this->attrOptionCollectionFactory = $attrOptionCollectionFactory; + } + + /** + * @param OptionManagement $subject + * @param int $entityType + * @param string $attributeCode + * @param AttributeOptionInterface $option + * + * @return [] + * @throws \Magento\Framework\Exception\LocalizedException + */ + public function beforeAdd(OptionManagement $subject, $entityType, $attributeCode, $option) + { + $attribute = $this->eavConfig->getAttribute(Product::ENTITY, $attributeCode); + + $isSwatch = false; + if ($this->swatchHelper->isVisualSwatch($attribute)) { + $isSwatch = true; + $optionKey = 'optionvisual'; + $swatchKey = 'swatchvisual'; + } elseif ($this->swatchHelper->isTextSwatch($attribute)) { + $isSwatch = true; + $optionKey = 'optiontext'; + $swatchKey = 'swatchtext'; + } + + if ($isSwatch && $attribute->getData($optionKey) === null && $attribute->getData($swatchKey) === null) { + $optionId = $option->getValue(); + $optionOrder = $option->getSortOrder(); + + $prefix = ''; + if ($optionId === '') { + $prefix = 'option_'; + $options = $this->getOptionsByAttributeIdWithSortOrder($attribute->getAttributeId()); + $attributeData = $this->getOptionsForSwatch($options, $optionKey); + $optionId = count($attributeData[$optionKey]['value']); + if ($optionOrder === null) { + $optionOrder = $optionId + 1; + } + $option->setValue($prefix . $optionId); + } + + + $storeLabels = $option->getStoreLabels(); + $attributeData[$optionKey]['delete'][$prefix . $optionId] = ''; + $attributeData[$optionKey]['order'][$prefix . $optionId] = $optionOrder; + if ($swatchKey === 'swatchvisual') { + $attributeData[$swatchKey]['value'][$prefix . $optionId] = ''; + } + foreach ($storeLabels as $storeLabel) { + $attributeData[$optionKey]['value'][$prefix . $optionId][$storeLabel->getStoreId()] + = $storeLabel->getLabel(); + if ($swatchKey === 'swatchtext') { + $attributeData[$swatchKey]['value'][$prefix . $optionId][$storeLabel->getStoreId()] + = $storeLabel->getLabel(); + } + } + $attribute->addData($attributeData); + } + + return [$entityType, $attributeCode, $option]; + } + + + /** + * @param [] $options + * @param string $optionKey + * + * @return array + */ + protected function getOptionsForSwatch($options, $optionKey) + { + $optionsArray = []; + + if (count($options) === 0) { + $optionsArray[$optionKey]['value'] = []; + $optionsArray[$optionKey]['delete'] = []; + + return $optionsArray; + } + foreach ($options as $sortOrder => $optionId) { + $optionsArray[$optionKey]['value'][$optionId] = $this->getStoreLabels($optionId); + $optionsArray[$optionKey]['delete'][$optionId] = ''; + $optionsArray[$optionKey]['order'][$optionId] = (string)$sortOrder; + } + + return $optionsArray; + } + + + /** + * @param $optionId + * + * @return array + */ + protected function getStoreLabels($optionId) + { + $attrOptionCollectionFactory = $this->attrOptionCollectionFactory->create(); + $connection = $attrOptionCollectionFactory->getConnection(); + $eavAttributeOptionValue = $attrOptionCollectionFactory->getTable('eav_attribute_option_value'); + $select = $connection->select()->from(['eaov' => $eavAttributeOptionValue], [])->where( + 'option_id = ?', + $optionId + )->columns(['store_id', 'value']); + + return $connection->fetchPairs($select); + } + + /** + * @param $attributeId + * + * @return array + */ + protected function getOptionsByAttributeIdWithSortOrder($attributeId) + { + $attrOptionCollectionFactory = $this->attrOptionCollectionFactory->create(); + $connection = $attrOptionCollectionFactory->getConnection(); + $eavAttributeOption = $attrOptionCollectionFactory->getTable('eav_attribute_option'); + $select = $connection->select()->from( + ['eao' => $eavAttributeOption], + ['option_id', 'sort_order'] + )->where('eao.attribute_Id = ? ', $attributeId)->order('eao.sort_order ASC'); + + return $connection->fetchCol($select); + } +} \ No newline at end of file diff --git a/app/code/Magento/Swatches/etc/di.xml b/app/code/Magento/Swatches/etc/di.xml index 5292bfafb6a0f..f64fa40edd684 100644 --- a/app/code/Magento/Swatches/etc/di.xml +++ b/app/code/Magento/Swatches/etc/di.xml @@ -81,4 +81,7 @@ + + + diff --git a/dev/tests/api-functional/testsuite/Magento/Swatches/Api/ProductAttributeOptionManagementInterfaceTest.php b/dev/tests/api-functional/testsuite/Magento/Swatches/Api/ProductAttributeOptionManagementInterfaceTest.php new file mode 100644 index 0000000000000..cbc82db68b32a --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/Swatches/Api/ProductAttributeOptionManagementInterfaceTest.php @@ -0,0 +1,102 @@ + [ + 'resourcePath' => self::RESOURCE_PATH . '/' . $testAttributeCode . '/options', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'add', + ], + ]; + + $response = $this->_webApiCall( + $serviceInfo, + [ + 'attributeCode' => $testAttributeCode, + 'option' => $optionData, + ] + ); + + $this->assertTrue($response); + $updatedData = $this->getAttributeOptions($testAttributeCode); + $lastOption = array_pop($updatedData); + $this->assertEquals( + $optionData[AttributeOptionInterface::STORE_LABELS][1][AttributeOptionLabelInterface::LABEL], + $lastOption['label'] + ); + } + + /** + * @return array + */ + public function addDataProvider() + { + $optionPayload = [ + AttributeOptionInterface::LABEL => 'new color', + AttributeOptionInterface::SORT_ORDER => 100, + AttributeOptionInterface::IS_DEFAULT => true, + AttributeOptionInterface::STORE_LABELS => [ + [ + AttributeOptionLabelInterface::LABEL => 'new color', + AttributeOptionLabelInterface::STORE_ID => 0, + ], + [ + AttributeOptionLabelInterface::LABEL => 'DE label', + AttributeOptionLabelInterface::STORE_ID => 1, + ], + ], + AttributeOptionInterface::VALUE => '' + ]; + + return [ + 'option_without_value_node' => [ + $optionPayload + ] + ]; + } + + /** + * @param $testAttributeCode + * @return array|bool|float|int|string + */ + private function getAttributeOptions($testAttributeCode) + { + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . '/' . $testAttributeCode . '/options', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'getItems', + ], + ]; + return $this->_webApiCall($serviceInfo, ['attributeCode' => $testAttributeCode]); + } +} From b5555ea1c573409cc2f7d01298d528216ba85546 Mon Sep 17 00:00:00 2001 From: andra lungu Date: Thu, 26 Oct 2017 22:51:00 +0300 Subject: [PATCH 2/6] fix phpmd error Cyclomatic Complexity of 11 fix unused parameters --- .../Catalog/AddSwatchDataToAddOption.php | 86 ++++++++++++------- 1 file changed, 56 insertions(+), 30 deletions(-) diff --git a/app/code/Magento/Swatches/Plugin/Catalog/AddSwatchDataToAddOption.php b/app/code/Magento/Swatches/Plugin/Catalog/AddSwatchDataToAddOption.php index 51b661b45bb36..cbb9c1d53ecc7 100644 --- a/app/code/Magento/Swatches/Plugin/Catalog/AddSwatchDataToAddOption.php +++ b/app/code/Magento/Swatches/Plugin/Catalog/AddSwatchDataToAddOption.php @@ -6,15 +6,20 @@ namespace Magento\Swatches\Plugin\Catalog; -use Magento\Eav\Model\Entity\Attribute\OptionManagement; -use Magento\Eav\Model\Config; use Magento\Catalog\Model\Product; -use Magento\Swatches\Helper\Data as SwatchHelper; use Magento\Eav\Api\Data\AttributeOptionInterface; +use Magento\Eav\Model\Config; +use Magento\Eav\Model\Entity\Attribute\OptionManagement; use Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory as OptionCollection; +use Magento\Swatches\Helper\Data as SwatchHelper; class AddSwatchDataToAddOption { + const SWATCH_VISUAL = 'visual'; + const SWATCH_TEXT = 'text'; + const PREFIX_OPTION = 'option'; + const PREFIX_SWATCH = 'swatch'; + /** * @var Config */ @@ -53,50 +58,44 @@ public function __construct( * * @return [] * @throws \Magento\Framework\Exception\LocalizedException + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function beforeAdd(OptionManagement $subject, $entityType, $attributeCode, $option) { $attribute = $this->eavConfig->getAttribute(Product::ENTITY, $attributeCode); - $isSwatch = false; - if ($this->swatchHelper->isVisualSwatch($attribute)) { - $isSwatch = true; - $optionKey = 'optionvisual'; - $swatchKey = 'swatchvisual'; - } elseif ($this->swatchHelper->isTextSwatch($attribute)) { - $isSwatch = true; - $optionKey = 'optiontext'; - $swatchKey = 'swatchtext'; - } + $isSwatch = $this->isSwatch($attribute); + $optionKey = self::PREFIX_OPTION . $isSwatch; + $swatchKey = self::PREFIX_SWATCH . $isSwatch; - if ($isSwatch && $attribute->getData($optionKey) === null && $attribute->getData($swatchKey) === null) { + if (!empty($isSwatch) && $attribute->getData($optionKey) === null + && $attribute->getData($swatchKey) === null) { $optionId = $option->getValue(); $optionOrder = $option->getSortOrder(); - $prefix = ''; if ($optionId === '') { - $prefix = 'option_'; - $options = $this->getOptionsByAttributeIdWithSortOrder($attribute->getAttributeId()); - $attributeData = $this->getOptionsForSwatch($options, $optionKey); + $attributeData = $this->prepareAttributeDataForNewOption($attribute->getAttributeId(), $optionKey); $optionId = count($attributeData[$optionKey]['value']); if ($optionOrder === null) { $optionOrder = $optionId + 1; } - $option->setValue($prefix . $optionId); + $prefix = 'option_' . $optionId; + $option->setValue($prefix); + } else { + $prefix = $optionId; } - - $storeLabels = $option->getStoreLabels(); - $attributeData[$optionKey]['delete'][$prefix . $optionId] = ''; - $attributeData[$optionKey]['order'][$prefix . $optionId] = $optionOrder; - if ($swatchKey === 'swatchvisual') { - $attributeData[$swatchKey]['value'][$prefix . $optionId] = ''; + $storeLabels = $option->getStoreLabels(); + $attributeData[$optionKey]['delete'][$prefix] = ''; + $attributeData[$optionKey]['order'][$prefix] = $optionOrder; + if ($isSwatch === self::SWATCH_VISUAL) { + $attributeData[$swatchKey]['value'][$prefix] = ''; } foreach ($storeLabels as $storeLabel) { - $attributeData[$optionKey]['value'][$prefix . $optionId][$storeLabel->getStoreId()] + $attributeData[$optionKey]['value'][$prefix][$storeLabel->getStoreId()] = $storeLabel->getLabel(); - if ($swatchKey === 'swatchtext') { - $attributeData[$swatchKey]['value'][$prefix . $optionId][$storeLabel->getStoreId()] + if ($isSwatch === self::SWATCH_TEXT) { + $attributeData[$swatchKey]['value'][$prefix][$storeLabel->getStoreId()] = $storeLabel->getLabel(); } } @@ -106,6 +105,33 @@ public function beforeAdd(OptionManagement $subject, $entityType, $attributeCode return [$entityType, $attributeCode, $option]; } + /** + * @param $attribute + * + * @return null|string + */ + protected function isSwatch($attribute) + { + if ($this->swatchHelper->isVisualSwatch($attribute)) { + return self::SWATCH_VISUAL; + } elseif ($this->swatchHelper->isTextSwatch($attribute)) { + return self::SWATCH_TEXT; + } + return null; + } + + /** + * @param $attributeId + * @param $optionKey + * + * @return array + */ + protected function prepareAttributeDataForNewOption($attributeId, $optionKey) + { + $options = $this->getOptionsByAttributeIdWithSortOrder($attributeId); + $attributeData = $this->getOptionsForSwatch($options, $optionKey); + return $attributeData; + } /** * @param [] $options @@ -123,6 +149,7 @@ protected function getOptionsForSwatch($options, $optionKey) return $optionsArray; } + foreach ($options as $sortOrder => $optionId) { $optionsArray[$optionKey]['value'][$optionId] = $this->getStoreLabels($optionId); $optionsArray[$optionKey]['delete'][$optionId] = ''; @@ -132,7 +159,6 @@ protected function getOptionsForSwatch($options, $optionKey) return $optionsArray; } - /** * @param $optionId * @@ -168,4 +194,4 @@ protected function getOptionsByAttributeIdWithSortOrder($attributeId) return $connection->fetchCol($select); } -} \ No newline at end of file +} From 1adb13b452580d27b588a0557696dedeac025d67 Mon Sep 17 00:00:00 2001 From: andra lungu Date: Fri, 27 Oct 2017 07:32:54 +0300 Subject: [PATCH 3/6] fix codacy issues --- .../Catalog/AddSwatchDataToAddOption.php | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/app/code/Magento/Swatches/Plugin/Catalog/AddSwatchDataToAddOption.php b/app/code/Magento/Swatches/Plugin/Catalog/AddSwatchDataToAddOption.php index cbb9c1d53ecc7..0cbe28eb0ea74 100644 --- a/app/code/Magento/Swatches/Plugin/Catalog/AddSwatchDataToAddOption.php +++ b/app/code/Magento/Swatches/Plugin/Catalog/AddSwatchDataToAddOption.php @@ -33,7 +33,7 @@ class AddSwatchDataToAddOption /** * @var OptionCollection */ - protected $attrOptionCollectionFactory; + protected $optionCollection; /** * @param Config $eavConfig @@ -47,7 +47,7 @@ public function __construct( ) { $this->eavConfig = $eavConfig; $this->swatchHelper = $swatchHelper; - $this->attrOptionCollectionFactory = $attrOptionCollectionFactory; + $this->optionCollection = $attrOptionCollectionFactory; } /** @@ -72,7 +72,7 @@ public function beforeAdd(OptionManagement $subject, $entityType, $attributeCode && $attribute->getData($swatchKey) === null) { $optionId = $option->getValue(); $optionOrder = $option->getSortOrder(); - + $prefix = $optionId; if ($optionId === '') { $attributeData = $this->prepareAttributeDataForNewOption($attribute->getAttributeId(), $optionKey); $optionId = count($attributeData[$optionKey]['value']); @@ -81,8 +81,6 @@ public function beforeAdd(OptionManagement $subject, $entityType, $attributeCode } $prefix = 'option_' . $optionId; $option->setValue($prefix); - } else { - $prefix = $optionId; } $storeLabels = $option->getStoreLabels(); @@ -166,10 +164,10 @@ protected function getOptionsForSwatch($options, $optionKey) */ protected function getStoreLabels($optionId) { - $attrOptionCollectionFactory = $this->attrOptionCollectionFactory->create(); - $connection = $attrOptionCollectionFactory->getConnection(); - $eavAttributeOptionValue = $attrOptionCollectionFactory->getTable('eav_attribute_option_value'); - $select = $connection->select()->from(['eaov' => $eavAttributeOptionValue], [])->where( + $optionCollection = $this->optionCollection->create(); + $connection = $optionCollection->getConnection(); + $optionValueTable = $optionCollection->getTable('eav_attribute_option_value'); + $select = $connection->select()->from(['eaov' => $optionValueTable], [])->where( 'option_id = ?', $optionId )->columns(['store_id', 'value']); @@ -184,11 +182,11 @@ protected function getStoreLabels($optionId) */ protected function getOptionsByAttributeIdWithSortOrder($attributeId) { - $attrOptionCollectionFactory = $this->attrOptionCollectionFactory->create(); - $connection = $attrOptionCollectionFactory->getConnection(); - $eavAttributeOption = $attrOptionCollectionFactory->getTable('eav_attribute_option'); + $optionCollection = $this->optionCollection->create(); + $connection = $optionCollection->getConnection(); + $optionTable = $optionCollection->getTable('eav_attribute_option'); $select = $connection->select()->from( - ['eao' => $eavAttributeOption], + ['eao' => $optionTable], ['option_id', 'sort_order'] )->where('eao.attribute_Id = ? ', $attributeId)->order('eao.sort_order ASC'); From a6aae87d1e31a28e1fce0c2b43e90077bbe0550f Mon Sep 17 00:00:00 2001 From: andra lungu Date: Sun, 29 Oct 2017 10:43:09 +0100 Subject: [PATCH 4/6] fix code review requests --- .../Catalog/AddSwatchDataToAddOption.php | 168 ++++++++++-------- 1 file changed, 96 insertions(+), 72 deletions(-) diff --git a/app/code/Magento/Swatches/Plugin/Catalog/AddSwatchDataToAddOption.php b/app/code/Magento/Swatches/Plugin/Catalog/AddSwatchDataToAddOption.php index 0cbe28eb0ea74..5693a00aa74fc 100644 --- a/app/code/Magento/Swatches/Plugin/Catalog/AddSwatchDataToAddOption.php +++ b/app/code/Magento/Swatches/Plugin/Catalog/AddSwatchDataToAddOption.php @@ -10,9 +10,14 @@ use Magento\Eav\Api\Data\AttributeOptionInterface; use Magento\Eav\Model\Config; use Magento\Eav\Model\Entity\Attribute\OptionManagement; -use Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory as OptionCollection; +use Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory as OptionCollectionFactory; use Magento\Swatches\Helper\Data as SwatchHelper; +/** + * Class AddSwatchDataToAddOption + * + * @package Magento\Swatches\Plugin\Catalog + */ class AddSwatchDataToAddOption { const SWATCH_VISUAL = 'visual'; @@ -23,40 +28,40 @@ class AddSwatchDataToAddOption /** * @var Config */ - protected $eavConfig; + private $eavConfig; /** * @var SwatchHelper */ - protected $swatchHelper; + private $swatchHelper; /** - * @var OptionCollection + * @var OptionCollectionFactory */ - protected $optionCollection; + private $optionCollectionFactory; /** - * @param Config $eavConfig - * @param SwatchHelper $swatchHelper - * @param OptionCollection $attrOptionCollectionFactory + * @param Config $eavConfig + * @param SwatchHelper $swatchHelper + * @param OptionCollectionFactory $attrOptionCollectionFactory */ public function __construct( Config $eavConfig, SwatchHelper $swatchHelper, - OptionCollection $attrOptionCollectionFactory + OptionCollectionFactory $attrOptionCollectionFactory ) { - $this->eavConfig = $eavConfig; - $this->swatchHelper = $swatchHelper; - $this->optionCollection = $attrOptionCollectionFactory; + $this->eavConfig = $eavConfig; + $this->swatchHelper = $swatchHelper; + $this->optionCollectionFactory = $attrOptionCollectionFactory; } /** - * @param OptionManagement $subject - * @param int $entityType - * @param string $attributeCode + * @param OptionManagement $subject + * @param int $entityType + * @param string $attributeCode * @param AttributeOptionInterface $option * - * @return [] + * @return array [] * @throws \Magento\Framework\Exception\LocalizedException * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ @@ -64,41 +69,45 @@ public function beforeAdd(OptionManagement $subject, $entityType, $attributeCode { $attribute = $this->eavConfig->getAttribute(Product::ENTITY, $attributeCode); - $isSwatch = $this->isSwatch($attribute); - $optionKey = self::PREFIX_OPTION . $isSwatch; - $swatchKey = self::PREFIX_SWATCH . $isSwatch; - - if (!empty($isSwatch) && $attribute->getData($optionKey) === null - && $attribute->getData($swatchKey) === null) { - $optionId = $option->getValue(); - $optionOrder = $option->getSortOrder(); - $prefix = $optionId; - if ($optionId === '') { - $attributeData = $this->prepareAttributeDataForNewOption($attribute->getAttributeId(), $optionKey); - $optionId = count($attributeData[$optionKey]['value']); - if ($optionOrder === null) { - $optionOrder = $optionId + 1; - } - $prefix = 'option_' . $optionId; - $option->setValue($prefix); - } + if (! $this->isSwatch($attribute)) { + return [$entityType, $attributeCode, $option]; + } + + $swatchType = $this->getSwatchType($attribute); + $optionKey = self::PREFIX_OPTION . $swatchType; + $swatchKey = self::PREFIX_SWATCH . $swatchType; + + if ($attribute->getData($optionKey) !== null || $attribute->getData($swatchKey) !== null) { + return [$entityType, $attributeCode, $option]; + } - $storeLabels = $option->getStoreLabels(); - $attributeData[$optionKey]['delete'][$prefix] = ''; - $attributeData[$optionKey]['order'][$prefix] = $optionOrder; - if ($isSwatch === self::SWATCH_VISUAL) { - $attributeData[$swatchKey]['value'][$prefix] = ''; + $optionId = $option->getValue(); + $optionOrder = $option->getSortOrder(); + $prefix = $optionId; + if ($optionId === '') { + $attributeData = $this->prepareAttributeDataForNewOption($attribute->getAttributeId(), $optionKey); + $optionId = count($attributeData[$optionKey]['value']); + if ($optionOrder === null) { + $optionOrder = $optionId + 1; } - foreach ($storeLabels as $storeLabel) { - $attributeData[$optionKey]['value'][$prefix][$storeLabel->getStoreId()] - = $storeLabel->getLabel(); - if ($isSwatch === self::SWATCH_TEXT) { - $attributeData[$swatchKey]['value'][$prefix][$storeLabel->getStoreId()] - = $storeLabel->getLabel(); - } + $prefix = 'option_' . $optionId; + $option->setValue($prefix); + } + + $storeLabels = $option->getStoreLabels(); + $attributeData[$optionKey]['delete'][$prefix] = ''; + $attributeData[$optionKey]['order'][$prefix] = $optionOrder; + if ($swatchType === self::SWATCH_VISUAL) { + $attributeData[$swatchKey]['value'][$prefix] = ''; + } + foreach ($storeLabels as $storeLabel) { + $attributeData[$optionKey]['value'][$prefix][$storeLabel->getStoreId()] = $storeLabel->getLabel(); + if ($swatchType === self::SWATCH_TEXT) { + $attributeData[$swatchKey]['value'][$prefix][$storeLabel->getStoreId()] = $storeLabel->getLabel(); } - $attribute->addData($attributeData); } + $attribute->addData($attributeData); + return [$entityType, $attributeCode, $option]; } @@ -106,29 +115,45 @@ public function beforeAdd(OptionManagement $subject, $entityType, $attributeCode /** * @param $attribute * - * @return null|string + * @return boolean */ protected function isSwatch($attribute) + { + if ($this->swatchHelper->isVisualSwatch($attribute)) { + return true; + } elseif ($this->swatchHelper->isTextSwatch($attribute)) { + return true; + } + + return false; + } + + /** + * @param $attribute + * + * @return null|string + */ + protected function getSwatchType($attribute) { if ($this->swatchHelper->isVisualSwatch($attribute)) { return self::SWATCH_VISUAL; } elseif ($this->swatchHelper->isTextSwatch($attribute)) { return self::SWATCH_TEXT; } + return null; } /** - * @param $attributeId - * @param $optionKey + * @param int $attributeId + * @param string $optionKey * * @return array */ protected function prepareAttributeDataForNewOption($attributeId, $optionKey) { - $options = $this->getOptionsByAttributeIdWithSortOrder($attributeId); - $attributeData = $this->getOptionsForSwatch($options, $optionKey); - return $attributeData; + $options = $this->getOptionsByAttributeIdWithSortOrder($attributeId); + return $this->getOptionsForSwatch($options, $optionKey); } /** @@ -137,7 +162,7 @@ protected function prepareAttributeDataForNewOption($attributeId, $optionKey) * * @return array */ - protected function getOptionsForSwatch($options, $optionKey) + protected function getOptionsForSwatch(array $options, $optionKey) { $optionsArray = []; @@ -158,38 +183,37 @@ protected function getOptionsForSwatch($options, $optionKey) } /** - * @param $optionId + * @param int $optionId * * @return array */ protected function getStoreLabels($optionId) { - $optionCollection = $this->optionCollection->create(); - $connection = $optionCollection->getConnection(); - $optionValueTable = $optionCollection->getTable('eav_attribute_option_value'); - $select = $connection->select()->from(['eaov' => $optionValueTable], [])->where( - 'option_id = ?', - $optionId - )->columns(['store_id', 'value']); + $optionCollectionFactory = $this->optionCollectionFactory->create(); + $connection = $optionCollectionFactory->getConnection(); + $optionValueTable = $optionCollectionFactory->getTable('eav_attribute_option_value'); + $select = $connection->select()->from( + ['eaov' => $optionValueTable], + [] + )->where('option_id = ?', $optionId)->columns(['store_id', 'value']); return $connection->fetchPairs($select); } /** - * @param $attributeId + * @param int $attributeId * * @return array */ protected function getOptionsByAttributeIdWithSortOrder($attributeId) { - $optionCollection = $this->optionCollection->create(); - $connection = $optionCollection->getConnection(); - $optionTable = $optionCollection->getTable('eav_attribute_option'); - $select = $connection->select()->from( - ['eao' => $optionTable], - ['option_id', 'sort_order'] - )->where('eao.attribute_Id = ? ', $attributeId)->order('eao.sort_order ASC'); - - return $connection->fetchCol($select); + $optionCollectionFactory = $this->optionCollectionFactory->create(); + $options = $optionCollectionFactory + ->setAttributeFilter($attributeId) + ->setPositionOrder() + ->addFieldToSelect('option_id') + ->getAllIds(); + + return array_values($options); } } From 2e8afa8353596ce396b44388b223e3d9087e48a3 Mon Sep 17 00:00:00 2001 From: David Manners Date: Wed, 22 Nov 2017 11:31:55 +0000 Subject: [PATCH 5/6] Remove extract empty line in the AddSwatchDataToAddOption plugin --- .../Magento/Swatches/Plugin/Catalog/AddSwatchDataToAddOption.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/code/Magento/Swatches/Plugin/Catalog/AddSwatchDataToAddOption.php b/app/code/Magento/Swatches/Plugin/Catalog/AddSwatchDataToAddOption.php index 5693a00aa74fc..60b3099a61264 100644 --- a/app/code/Magento/Swatches/Plugin/Catalog/AddSwatchDataToAddOption.php +++ b/app/code/Magento/Swatches/Plugin/Catalog/AddSwatchDataToAddOption.php @@ -108,7 +108,6 @@ public function beforeAdd(OptionManagement $subject, $entityType, $attributeCode } $attribute->addData($attributeData); - return [$entityType, $attributeCode, $option]; } From 5e367ed104cfb6ec08913ca3828a018420534a55 Mon Sep 17 00:00:00 2001 From: David Manners Date: Wed, 22 Nov 2017 12:52:14 +0000 Subject: [PATCH 6/6] Update methods from protected to private --- .../Plugin/Catalog/AddSwatchDataToAddOption.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/code/Magento/Swatches/Plugin/Catalog/AddSwatchDataToAddOption.php b/app/code/Magento/Swatches/Plugin/Catalog/AddSwatchDataToAddOption.php index 60b3099a61264..7a6446e9fad3e 100644 --- a/app/code/Magento/Swatches/Plugin/Catalog/AddSwatchDataToAddOption.php +++ b/app/code/Magento/Swatches/Plugin/Catalog/AddSwatchDataToAddOption.php @@ -116,7 +116,7 @@ public function beforeAdd(OptionManagement $subject, $entityType, $attributeCode * * @return boolean */ - protected function isSwatch($attribute) + private function isSwatch($attribute) { if ($this->swatchHelper->isVisualSwatch($attribute)) { return true; @@ -132,7 +132,7 @@ protected function isSwatch($attribute) * * @return null|string */ - protected function getSwatchType($attribute) + private function getSwatchType($attribute) { if ($this->swatchHelper->isVisualSwatch($attribute)) { return self::SWATCH_VISUAL; @@ -149,7 +149,7 @@ protected function getSwatchType($attribute) * * @return array */ - protected function prepareAttributeDataForNewOption($attributeId, $optionKey) + private function prepareAttributeDataForNewOption($attributeId, $optionKey) { $options = $this->getOptionsByAttributeIdWithSortOrder($attributeId); return $this->getOptionsForSwatch($options, $optionKey); @@ -161,7 +161,7 @@ protected function prepareAttributeDataForNewOption($attributeId, $optionKey) * * @return array */ - protected function getOptionsForSwatch(array $options, $optionKey) + private function getOptionsForSwatch(array $options, $optionKey) { $optionsArray = []; @@ -186,7 +186,7 @@ protected function getOptionsForSwatch(array $options, $optionKey) * * @return array */ - protected function getStoreLabels($optionId) + private function getStoreLabels($optionId) { $optionCollectionFactory = $this->optionCollectionFactory->create(); $connection = $optionCollectionFactory->getConnection(); @@ -204,7 +204,7 @@ protected function getStoreLabels($optionId) * * @return array */ - protected function getOptionsByAttributeIdWithSortOrder($attributeId) + private function getOptionsByAttributeIdWithSortOrder($attributeId) { $optionCollectionFactory = $this->optionCollectionFactory->create(); $options = $optionCollectionFactory