I am trying to create a configurable product using the following code:
// Load Configurable Product
$product = $this->product->create();
$product = $product->load($parentId);
$attributeValues = [];
$options = $this->attribute->getOptions();
array_shift($options); //remove the first option which is empty
foreach($options as $option) {
$attributeValues[] = [
'label' => 'Option',
'attribute_id' => $this->attribute->getId(),
'value_index' => $option->getValue(),
];
}
$configurableAttributesData = [
[
'attribute_id' => $this->attribute->getId(),
'code' => $this->attribute->getAttributeCode(),
'label' => $this->attribute->getStoreLabel(),
'position' => '1',
'values' => $attributeValues,
],
];
$this->output->writeln('ConfigurableOptions:'. print_r($configurableAttributesData, true));
$configurableOptions = $this->optionsFactory->create($configurableAttributesData);
$extensionConfigurableAttributes = $product->getExtensionAttributes();
$extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
$this->output->writeln("Linking deals:".print_r($simpleDeals['ids'], true));
$extensionConfigurableAttributes->setConfigurableProductLinks($simpleDeals['ids']);
$product->setExtensionAttributes($extensionConfigurableAttributes);
$this->output->writeln('Extension attributes set');
$this->productRepository->save($product);
All of the simple deals have been created at that point as well as the attribute options. This is what the $configurableAttributesData array looks like:
Array
(
[0] => Array
(
[attribute_id] => 215
[code] => deal_options
[label] => Option
[position] => 1
[values] => Array
(
[0] => Array
(
[label] => Option
[attribute_id] => 215
[value_index] => 128
)
[1] => Array
(
[label] => Option
[attribute_id] => 215
[value_index] => 129
)
[2] => Array
(
[label] => Option
[attribute_id] => 215
[value_index] => 130
)
)
)
)
But when running the code, everything runs smoothly except for the $this->productRepository->save($product); part which gives this error:
Option values are not specified.
Any idea what might cause this?
Thanks