Skip to content

Commit 55dee3f

Browse files
authored
ENGCOM-5385: Feature/9798 updating configurable product options based on produc id and sku #23529
2 parents dde0061 + 3833004 commit 55dee3f

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

app/code/Magento/ConfigurableProduct/Model/OptionRepository.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Magento\Store\Model\Store;
2323

2424
/**
25+
* Repository for performing CRUD operations for a configurable product's options.
2526
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2627
*/
2728
class OptionRepository implements \Magento\ConfigurableProduct\Api\OptionRepositoryInterface
@@ -112,7 +113,7 @@ public function __construct(
112113
}
113114

114115
/**
115-
* {@inheritdoc}
116+
* @inheritdoc
116117
*/
117118
public function get($sku, $id)
118119
{
@@ -131,7 +132,7 @@ public function get($sku, $id)
131132
}
132133

133134
/**
134-
* {@inheritdoc}
135+
* @inheritdoc
135136
*/
136137
public function getList($sku)
137138
{
@@ -141,7 +142,7 @@ public function getList($sku)
141142
}
142143

143144
/**
144-
* {@inheritdoc}
145+
* @inheritdoc
145146
*/
146147
public function delete(OptionInterface $option)
147148
{
@@ -167,7 +168,7 @@ public function delete(OptionInterface $option)
167168
}
168169

169170
/**
170-
* {@inheritdoc}
171+
* @inheritdoc
171172
*/
172173
public function deleteById($sku, $id)
173174
{
@@ -184,7 +185,7 @@ public function deleteById($sku, $id)
184185
}
185186

186187
/**
187-
* {@inheritdoc}
188+
* @inheritdoc
188189
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
189190
*/
190191
public function save($sku, OptionInterface $option)
@@ -213,6 +214,16 @@ public function save($sku, OptionInterface $option)
213214
throw new \InvalidArgumentException('Incompatible product type');
214215
}
215216
$option->setProductId($product->getData($metadata->getLinkField()));
217+
if (!empty($option->getProductId() && !empty($option->getAttributeId()))) {
218+
$id = $this->optionResource->getIdByProductIdAndAttributeId(
219+
$option,
220+
$option->getProductId(),
221+
$option->getAttributeId()
222+
);
223+
if (!empty($id)) {
224+
$option->setId($id);
225+
}
226+
}
216227
}
217228

218229
try {
@@ -296,6 +307,7 @@ public function validateNewOptionData(OptionInterface $option)
296307

297308
/**
298309
* Get MetadataPool instance
310+
*
299311
* @return MetadataPool
300312
*/
301313
private function getMetadataPool()

dev/tests/api-functional/testsuite/Magento/ConfigurableProduct/Api/OptionRepositoryTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
use Magento\TestFramework\Helper\Bootstrap;
1111
use Magento\Eav\Api\AttributeRepositoryInterface;
1212

13+
/**
14+
* Class OptionRepositoryTest for testing ConfigurableProductoption integration
15+
*/
1316
class OptionRepositoryTest extends \Magento\TestFramework\TestCase\WebapiAbstract
1417
{
1518
const SERVICE_NAME = 'configurableProductOptionRepositoryV1';
@@ -210,6 +213,47 @@ public function testUpdate()
210213
$this->assertEquals($requestBody['option']['label'], $configurableAttribute[0]['label']);
211214
}
212215

216+
/**
217+
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
218+
*/
219+
public function testUpdateWithoutOptionId()
220+
{
221+
$productSku = 'configurable';
222+
/** @var AttributeRepositoryInterface $attributeRepository */
223+
224+
$attributeRepository = Bootstrap::getObjectManager()->create(AttributeRepositoryInterface::class);
225+
226+
/** @var \Magento\Eav\Api\Data\AttributeInterface $attribute */
227+
$attribute = $attributeRepository->get('catalog_product', 'test_configurable');
228+
229+
$serviceInfo = [
230+
'rest' => [
231+
'resourcePath' => self::RESOURCE_PATH . '/' . $productSku . '/options',
232+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST
233+
],
234+
'soap' => [
235+
'service' => self::SERVICE_NAME,
236+
'serviceVersion' => self::SERVICE_VERSION,
237+
'operation' => self::SERVICE_NAME . 'Save'
238+
]
239+
];
240+
241+
$option = [
242+
'attribute_id' => $attribute->getAttributeId(),
243+
'label' => 'Update Test Configurable with sku and attribute_id',
244+
'values' => [
245+
[
246+
'value_index' => 1,
247+
]
248+
],
249+
];
250+
251+
$result = $this->_webApiCall($serviceInfo, ['sku' => $productSku, 'option' => $option]);
252+
$this->assertGreaterThan(0, $result);
253+
$configurableAttribute = $this->getConfigurableAttribute($productSku);
254+
$this->assertEquals($option['label'], $configurableAttribute[0]['label']);
255+
}
256+
213257
/**
214258
* @param string $productSku
215259
* @return array

0 commit comments

Comments
 (0)