Skip to content

Commit 7d150f6

Browse files
author
cspruiell
committed
MAGETWO-58401: Cannot create configurable product with child by REST API - for 2.1.x
- retrieve attributes into collection - update test
1 parent 95ac4c1 commit 7d150f6

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed

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

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,15 @@ public function addChild($sku, $childSku)
117117
throw new StateException(__('Parent product does not have configurable product options'));
118118
}
119119

120-
$configurableOptionData = [];
120+
$attributeIds = [];
121121
foreach ($configurableProductOptions as $configurableProductOption) {
122122
$attributeCode = $configurableProductOption->getProductAttribute()->getAttributeCode();
123123
if (!$child->getData($attributeCode)) {
124124
throw new StateException(__('Child product does not have attribute value %1', $attributeCode));
125125
}
126-
$configurableOptionData = array_merge($configurableOptionData,
127-
$this->getConfigurableAttributesData($configurableProductOption->getAttributeId()));
126+
$attributeIds[] = $configurableProductOption->getAttributeId();
128127
}
128+
$configurableOptionData = $this->getConfigurableAttributesData($attributeIds);
129129

130130
/** @var Factory $optionFactory */
131131
$optionFactory = $this->getOptionsFactory();
@@ -201,33 +201,35 @@ private function getAttributeFactory()
201201
/**
202202
* Get Configurable Attribute Data
203203
*
204-
* @param int $attributeId
204+
* @param int[] $attributeIds
205205
* @return array
206206
*/
207-
private function getConfigurableAttributesData($attributeId)
207+
private function getConfigurableAttributesData($attributeIds)
208208
{
209209
$attributeValues = [];
210-
$attribute = $this->getAttributeFactory()->create();
211-
$attribute->load($attributeId);
212-
foreach ($attribute->getOptions() as $option) {
213-
if ($option->getValue()) {
214-
$attributeValues[] = [
215-
'label' => $option->getLabel(),
210+
$attributes = $this->getAttributeFactory()->create()
211+
->getCollection()
212+
->addFieldToFilter('attribute_id', $attributeIds)
213+
->getItems();
214+
foreach ($attributes as $attribute) {
215+
foreach ($attribute->getOptions() as $option) {
216+
if ($option->getValue()) {
217+
$attributeValues[] = [
218+
'label' => $option->getLabel(),
219+
'attribute_id' => $attribute->getId(),
220+
'value_index' => $option->getValue(),
221+
];
222+
}
223+
}
224+
$configurableAttributesData[] =
225+
[
216226
'attribute_id' => $attribute->getId(),
217-
'value_index' => $option->getValue(),
227+
'code' => $attribute->getAttributeCode(),
228+
'label' => $attribute->getStoreLabel(),
229+
'values' => $attributeValues,
218230
];
219-
}
220231
}
221232

222-
$configurableAttributesData = [
223-
[
224-
'attribute_id' => $attribute->getId(),
225-
'code' => $attribute->getAttributeCode(),
226-
'label' => $attribute->getStoreLabel(),
227-
'values' => $attributeValues,
228-
],
229-
];
230-
231233
return $configurableAttributesData;
232234
}
233235
}

app/code/Magento/ConfigurableProduct/Test/Unit/Model/LinkManagementTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,20 @@ public function testAddChild()
183183

184184
$attributeMock = $this->getMockBuilder('Magento\Catalog\Model\ResourceModel\Eav\Attribute')
185185
->disableOriginalConstructor()
186-
->setMethods(['load', 'getOptions', 'getId', 'getAttributeCode', 'getStoreLabel'])
186+
->setMethods(['getCollection', 'getOptions', 'getId', 'getAttributeCode', 'getStoreLabel'])
187187
->getMock();
188188
$attributeOptionMock = $this->getMockBuilder('Magento\Eav\Model\Entity\Attribute\Option')
189189
->disableOriginalConstructor()
190190
->setMethods(['getValue', 'getLabel'])
191191
->getMock();
192192

193+
$attributeCollectionMock = $this->getMockBuilder(
194+
'Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute\Collection'
195+
)
196+
->disableOriginalConstructor()
197+
->setMethods(['addFieldToFilter', 'getItems'])
198+
->getMock();
199+
193200
$this->productRepository->expects($this->at(0))->method('get')->with($productSku)->willReturn($configurable);
194201
$this->productRepository->expects($this->at(1))->method('get')->with($childSku)->willReturn($simple);
195202

@@ -212,7 +219,10 @@ public function testAddChild()
212219

213220
$optionsFactoryMock->expects($this->any())->method('create')->willReturn([$optionMock]);
214221
$attributeFactoryMock->expects($this->any())->method('create')->willReturn($attributeMock);
215-
$attributeMock->expects($this->any())->method('load');
222+
$attributeMock->expects($this->any())->method('getCollection')->willReturn($attributeCollectionMock);
223+
$attributeCollectionMock->expects($this->any())->method('addFieldToFilter')->willReturnSelf();
224+
$attributeCollectionMock->expects($this->any())->method('getItems')->willReturn([$attributeMock]);
225+
216226
$attributeMock->expects($this->any())->method('getOptions')->willReturn([$attributeOptionMock]);
217227

218228
$extensionAttributesMock->expects($this->any())->method('setConfigurableProductOptions');

0 commit comments

Comments
 (0)