0

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

0

Ok this error was caused by the simple deals not having values in the attribute used for the configurable deal connection to the simple deals.

I simply had to run this (with the option ID that you create for that deal):

$product->setCustomAttribute('deal_options',$optionId);

When creating the simple deals

1
  • Hi @maxl, Do you know include gallery_image into your data for save.
    – xanka
    Oct 20 '17 at 1:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.