|
8 | 8 |
|
9 | 9 | use Magento\Framework\Exception\InputException;
|
10 | 10 | use Magento\Framework\Exception\NoSuchEntityException;
|
| 11 | +use Magento\Eav\Api\Data\AttributeInterface; |
11 | 12 |
|
12 | 13 | /**
|
13 | 14 | * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
@@ -49,11 +50,6 @@ class Repository implements \Magento\Catalog\Api\ProductAttributeRepositoryInter
|
49 | 50 | */
|
50 | 51 | protected $searchCriteriaBuilder;
|
51 | 52 |
|
52 |
| - /** |
53 |
| - * @var \Magento\Catalog\Api\ProductAttributeOptionManagementInterface |
54 |
| - */ |
55 |
| - private $optionManagement; |
56 |
| - |
57 | 53 | /**
|
58 | 54 | * @param \Magento\Catalog\Model\ResourceModel\Attribute $attributeResource
|
59 | 55 | * @param \Magento\Catalog\Helper\Product $productHelper
|
@@ -117,12 +113,17 @@ public function save(\Magento\Catalog\Api\Data\ProductAttributeInterface $attrib
|
117 | 113 | throw NoSuchEntityException::singleField('attribute_code', $existingModel->getAttributeCode());
|
118 | 114 | }
|
119 | 115 |
|
| 116 | + // Attribute code must not be changed after attribute creation |
| 117 | + $attribute->setAttributeCode($existingModel->getAttributeCode()); |
120 | 118 | $attribute->setAttributeId($existingModel->getAttributeId());
|
121 | 119 | $attribute->setIsUserDefined($existingModel->getIsUserDefined());
|
122 | 120 | $attribute->setFrontendInput($existingModel->getFrontendInput());
|
123 | 121 |
|
124 | 122 | if (is_array($attribute->getFrontendLabels())) {
|
125 |
| - $frontendLabel[0] = $existingModel->getDefaultFrontendLabel(); |
| 123 | + $defaultFrontendLabel = $attribute->getDefaultFrontendLabel(); |
| 124 | + $frontendLabel[0] = !empty($defaultFrontendLabel) |
| 125 | + ? $defaultFrontendLabel |
| 126 | + : $existingModel->getDefaultFrontendLabel(); |
126 | 127 | foreach ($attribute->getFrontendLabels() as $item) {
|
127 | 128 | $frontendLabel[$item->getStoreId()] = $item->getLabel();
|
128 | 129 | }
|
@@ -171,10 +172,31 @@ public function save(\Magento\Catalog\Api\Data\ProductAttributeInterface $attrib
|
171 | 172 | );
|
172 | 173 | $attribute->setIsUserDefined(1);
|
173 | 174 | }
|
174 |
| - $this->attributeResource->save($attribute); |
175 |
| - foreach ($attribute->getOptions() as $option) { |
176 |
| - $this->getOptionManagement()->add($attribute->getAttributeCode(), $option); |
| 175 | + if (!empty($attribute->getData(AttributeInterface::OPTIONS))) { |
| 176 | + $options = []; |
| 177 | + $sortOrder = 0; |
| 178 | + $default = []; |
| 179 | + $optionIndex = 0; |
| 180 | + foreach ($attribute->getOptions() as $option) { |
| 181 | + $optionIndex++; |
| 182 | + $optionId = $option->getValue() ?: 'option_' . $optionIndex; |
| 183 | + $options['value'][$optionId][0] = $option->getLabel(); |
| 184 | + $options['order'][$optionId] = $option->getSortOrder() ?: $sortOrder++; |
| 185 | + if (is_array($option->getStoreLabels())) { |
| 186 | + foreach ($option->getStoreLabels() as $label) { |
| 187 | + $options['value'][$optionId][$label->getStoreId()] = $label->getLabel(); |
| 188 | + } |
| 189 | + } |
| 190 | + if ($option->getIsDefault()) { |
| 191 | + $default[] = $optionId; |
| 192 | + } |
| 193 | + } |
| 194 | + $attribute->setDefault($default); |
| 195 | + if (count($options)) { |
| 196 | + $attribute->setOption($options); |
| 197 | + } |
177 | 198 | }
|
| 199 | + $this->attributeResource->save($attribute); |
178 | 200 | return $this->get($attribute->getAttributeCode());
|
179 | 201 | }
|
180 | 202 |
|
@@ -253,16 +275,4 @@ protected function validateFrontendInput($frontendInput)
|
253 | 275 | throw InputException::invalidFieldValue('frontend_input', $frontendInput);
|
254 | 276 | }
|
255 | 277 | }
|
256 |
| - |
257 |
| - /** |
258 |
| - * @return \Magento\Catalog\Api\ProductAttributeOptionManagementInterface |
259 |
| - */ |
260 |
| - private function getOptionManagement() |
261 |
| - { |
262 |
| - if (null === $this->optionManagement) { |
263 |
| - $this->optionManagement = \Magento\Framework\App\ObjectManager::getInstance() |
264 |
| - ->get('Magento\Catalog\Api\ProductAttributeOptionManagementInterface'); |
265 |
| - } |
266 |
| - return $this->optionManagement; |
267 |
| - } |
268 | 278 | }
|
0 commit comments