Skip to content

Commit 07a4b3b

Browse files
#8810 - REST API - Attribute option creation -> no ID returned
1 parent bce891d commit 07a4b3b

File tree

4 files changed

+9
-5
lines changed

4 files changed

+9
-5
lines changed

app/code/Magento/Catalog/Api/ProductAttributeOptionManagementInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function getItems($attributeCode);
2929
* @param \Magento\Eav\Api\Data\AttributeOptionInterface $option
3030
* @throws \Magento\Framework\Exception\StateException
3131
* @throws \Magento\Framework\Exception\InputException
32-
* @return bool
32+
* @return \Magento\Eav\Api\Data\AttributeOptionInterface
3333
*/
3434
public function add($attributeCode, $option);
3535

app/code/Magento/Eav/Api/AttributeOptionManagementInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ interface AttributeOptionManagementInterface
2020
* @param \Magento\Eav\Api\Data\AttributeOptionInterface $option
2121
* @throws \Magento\Framework\Exception\StateException
2222
* @throws \Magento\Framework\Exception\InputException
23-
* @return bool
23+
* @return \Magento\Eav\Api\Data\AttributeOptionInterface
2424
*/
2525
public function add($entityType, $attributeCode, $option);
2626

app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ public function add($entityType, $attributeCode, $option)
4949
throw new StateException(__('Attribute %1 doesn\'t work with options', $attributeCode));
5050
}
5151

52+
$optionLabel = $option->getLabel();
5253
$optionId = $this->getOptionId($option);
5354
$options = [];
54-
$options['value'][$optionId][0] = $option->getLabel();
55+
$options['value'][$optionId][0] = $optionLabel;
5556
$options['order'][$optionId] = $option->getSortOrder();
5657

5758
if (is_array($option->getStoreLabels())) {
@@ -67,11 +68,14 @@ public function add($entityType, $attributeCode, $option)
6768
$attribute->setOption($options);
6869
try {
6970
$this->resourceModel->save($attribute);
71+
if ($optionLabel && $attribute->getAttributeCode()) {
72+
$option->setValue($attribute->getSource()->getOptionId($optionLabel));
73+
}
7074
} catch (\Exception $e) {
7175
throw new StateException(__('Cannot save attribute %1', $attributeCode));
7276
}
7377

74-
return true;
78+
return $option;
7579
}
7680

7781
/**

app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/OptionManagementTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function testAdd()
8080
$attributeMock->expects($this->once())->method('setDefault')->with(['new_option']);
8181
$attributeMock->expects($this->once())->method('setOption')->with($option);
8282
$this->resourceModelMock->expects($this->once())->method('save')->with($attributeMock);
83-
$this->assertTrue($this->model->add($entityType, $attributeCode, $optionMock));
83+
$this->assertEquals($optionMock, $this->model->add($entityType, $attributeCode, $optionMock));
8484
}
8585

8686
/**

0 commit comments

Comments
 (0)