Skip to content

Commit 114a8e5

Browse files
committed
fix: check if attribute code exists in the case we have no attribute id
1 parent c8ba4ab commit 114a8e5

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

app/code/Magento/Catalog/Model/Product/Attribute/Repository.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,18 @@ public function save(\Magento\Catalog\Api\Data\ProductAttributeInterface $attrib
133133
->getEntityType(\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE)
134134
->getId()
135135
);
136+
if (!$attribute->getAttributeId() && $attribute->getAttributeCode()) {
137+
try {
138+
$existingAttribute = $this->get($attribute->getAttributeCode());
139+
if ($existingAttribute->getAttributeId()) {
140+
$attribute->setAttributeId($existingAttribute->getAttributeId());
141+
}
142+
} catch (NoSuchEntityException $e) {
143+
// It's a new attribute, proceed as usual
144+
}
145+
}
146+
147+
$validOptionIds = [];
136148
if ($attribute->getAttributeId()) {
137149
$existingModel = $this->get($attribute->getAttributeCode());
138150

@@ -148,6 +160,13 @@ public function save(\Magento\Catalog\Api\Data\ProductAttributeInterface $attrib
148160
$attribute->setBackendModel($existingModel->getBackendModel());
149161

150162
$this->updateDefaultFrontendLabel($attribute, $existingModel);
163+
164+
$source = $existingModel->getSource();
165+
if ($source) {
166+
foreach ($source->getAllOptions() as $opt) {
167+
$validOptionIds[] = $opt['value'];
168+
}
169+
}
151170
} else {
152171
$attribute->setAttributeId(null);
153172

@@ -181,7 +200,11 @@ public function save(\Magento\Catalog\Api\Data\ProductAttributeInterface $attrib
181200
$optionIndex = 0;
182201
foreach ($attribute->getOptions() as $option) {
183202
$optionIndex++;
184-
$optionId = $option->getValue() ?: 'option_' . $optionIndex;
203+
$optionId = $option->getValue();
204+
if ($optionId && is_numeric($optionId) && !in_array($optionId, $validOptionIds)) {
205+
$optionId = null;
206+
}
207+
$optionId = $optionId ?: 'option_' . $optionIndex;
185208
$options['value'][$optionId][0] = $option->getLabel();
186209
$options['order'][$optionId] = $option->getSortOrder() ?: $sortOrder++;
187210
if (is_array($option->getStoreLabels())) {

app/code/Magento/Catalog/Test/Unit/Model/Product/Attribute/RepositoryTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,9 @@ public function testSaveNoSuchEntityException()
210210
$this->expectExceptionMessage('No such entity with attribute_code = test attribute code');
211211
$attributeMock = $this->createMock(Attribute::class);
212212
$existingModelMock = $this->createMock(Attribute::class);
213-
$attributeMock->expects($this->once())->method('getAttributeId')->willReturn('12');
213+
$attributeMock->expects($this->any())->method('getAttributeId')->willReturn('12');
214214
$attributeCode = 'test attribute code';
215-
$attributeMock->expects($this->once())->method('getAttributeCode')->willReturn($attributeCode);
215+
$attributeMock->expects($this->any())->method('getAttributeCode')->willReturn($attributeCode);
216216
$this->eavAttributeRepositoryMock->expects($this->once())
217217
->method('get')
218218
->with(
@@ -234,7 +234,7 @@ public function testSaveInputExceptionRequiredField()
234234
Attribute::class,
235235
['getFrontendLabels', 'getDefaultFrontendLabel', 'getAttributeId', 'setAttributeId']
236236
);
237-
$attributeMock->expects($this->once())->method('getAttributeId')->willReturn(null);
237+
$attributeMock->expects($this->any())->method('getAttributeId')->willReturn(null);
238238
$attributeMock->expects($this->once())->method('setAttributeId')->with(null)->willReturnSelf();
239239
$attributeMock->expects($this->once())->method('getFrontendLabels')->willReturn(null);
240240
$attributeMock->expects($this->once())->method('getDefaultFrontendLabel')->willReturn(null);
@@ -286,7 +286,7 @@ public function testSaveInputExceptionInvalidFieldValue()
286286
Attribute::class,
287287
['getFrontendLabels', 'getDefaultFrontendLabel', 'getAttributeId', 'setAttributeId']
288288
);
289-
$attributeMock->expects($this->once())->method('getAttributeId')->willReturn(null);
289+
$attributeMock->expects($this->any())->method('getAttributeId')->willReturn(null);
290290
$attributeMock->expects($this->once())->method('setAttributeId')->with(null)->willReturnSelf();
291291
$labelMock = $this->createMock(FrontendLabel::class);
292292
$attributeMock->expects($this->any())->method('getFrontendLabels')->willReturn([$labelMock]);

0 commit comments

Comments
 (0)