1

I trying to add simple products and after associate these products in a configurable product, but some options of these products can't be exists.

So, I add options using EVA config and return id of options, but these options have type like

Magento\Eav\Api\Data\AttributeOptionInterface

And

Magento\Catalog\Model\Product->addOptions()

or

Magento\Catalog\Model\Product->addOption()

requires object type like

Magento\Catalog\Model\Product\Option

I don't understand how Magento can create options like this. See that they already exist inside objects like

Magento\Eav\Model\Entity\Attribute\AbstractAttribute

I needed to associate my products with these options but I don't understand how I can do it.

Suppose the method works perfectly for the purpose of inserting options into the attributes and the option objects are returned from this call. Follow the code.

$options = $this->helper->setupAttributeOptions([
    "size" => $configurable["options"]["size"],
    "color" => $configurable["options"]["color"],
]);
/*
return ->
[
    "color" => [
        0 => [
            "label" => "Red",
            "option_object" => Magento\Eav\Api\Data\AttributeOptionInterface 
        ] 
    ],
    "size" => [
        0 => [
            "label" => 40,
            "option_object" => Magento\Eav\Api\Data\AttributeOptionInterface 
        ] 
    ]
]
*/

I parse result and extract option_object inside an other simple array. Load my simple product by SKU using Magento\Catalog\Api\ProductRepositoryInterface and add options with this.

$product->setHasOptions(1);
$product->setOptions($correctOptions);
$product->save();

I try using $productRepository->save($product); but not have success.

Any idea.

Your Answer

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

Browse other questions tagged or ask your own question.