Skip to content

Commit d2a73c8

Browse files
MAGETWO-86176: #8810 - REST API - Attribute option creation -> no ID returned #12920
- Merge Pull Request #12920 from sanjay-wagento/magento2:2.3-feature-rest-api-attribute-option-creation - Merged commits: 1. 07a4b3b 2. fc00146 3. b9e0abe 4. e31ea92 5. 9b57e79 6. 294e200 7. 5eef1d9
2 parents 23df0f3 + 5eef1d9 commit d2a73c8

File tree

6 files changed

+36
-8
lines changed

6 files changed

+36
-8
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 string
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 string
2424
*/
2525
public function add($entityType, $attributeCode, $option);
2626

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ public function add($entityType, $attributeCode, $option)
4949
throw new StateException(__('The "%1" attribute 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+
$this->setOptionValue($option, $attribute, $optionLabel);
73+
}
7074
} catch (\Exception $e) {
7175
throw new StateException(__('The "%1" attribute can\'t be saved.', $attributeCode));
7276
}
7377

74-
return true;
78+
return $this->getOptionId($option);
7579
}
7680

7781
/**
@@ -147,8 +151,32 @@ protected function validateOption($attribute, $optionId)
147151
* @param \Magento\Eav\Api\Data\AttributeOptionInterface $option
148152
* @return string
149153
*/
150-
private function getOptionId($option)
154+
private function getOptionId(\Magento\Eav\Api\Data\AttributeOptionInterface $option) : string
151155
{
152156
return $option->getValue() ?: 'new_option';
153157
}
158+
159+
/**
160+
* @param \Magento\Eav\Api\Data\AttributeOptionInterface $option
161+
* @param \Magento\Eav\Api\Data\AttributeInterface $attribute
162+
* @param string $optionLabel
163+
* @return void
164+
*/
165+
private function setOptionValue(
166+
\Magento\Eav\Api\Data\AttributeOptionInterface $option,
167+
\Magento\Eav\Api\Data\AttributeInterface $attribute,
168+
string $optionLabel
169+
) {
170+
$optionId = $attribute->getSource()->getOptionId($optionLabel);
171+
if ($optionId) {
172+
$option->setValue($attribute->getSource()->getOptionId($optionId));
173+
} elseif (is_array($option->getStoreLabels())) {
174+
foreach ($option->getStoreLabels() as $label) {
175+
if ($optionId = $attribute->getSource()->getOptionId($label->getLabel())) {
176+
$option->setValue($attribute->getSource()->getOptionId($optionId));
177+
break;
178+
}
179+
}
180+
}
181+
}
154182
}

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
@@ -81,7 +81,7 @@ public function testAdd()
8181
$attributeMock->expects($this->once())->method('setDefault')->with(['new_option']);
8282
$attributeMock->expects($this->once())->method('setOption')->with($option);
8383
$this->resourceModelMock->expects($this->once())->method('save')->with($attributeMock);
84-
$this->assertTrue($this->model->add($entityType, $attributeCode, $optionMock));
84+
$this->assertEquals('new_option', $this->model->add($entityType, $attributeCode, $optionMock));
8585
}
8686

8787
/**

dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductAttributeOptionManagementInterfaceTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function testAdd($optionData)
7474
]
7575
);
7676

77-
$this->assertTrue($response);
77+
$this->assertNotNull($response);
7878
$updatedData = $this->getAttributeOptions($testAttributeCode);
7979
$lastOption = array_pop($updatedData);
8080
$this->assertEquals(

dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductSwatchAttributeOptionManagementInterfaceTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function testAdd($optionData)
4242
]
4343
);
4444

45-
$this->assertTrue($response);
45+
$this->assertNotNull($response);
4646
$updatedData = $this->getAttributeOptions($testAttributeCode);
4747
$lastOption = array_pop($updatedData);
4848
$this->assertEquals(

0 commit comments

Comments
 (0)