Zend certified PHP/Magento developer

Programmatically link configurable product with simple product threw options

I have a configurable product created and also simple products.

I want to add the option on the forme and poids attribute to link them.
Thse option are properly configured, they appear in the admin if I want to add from admin

options

But i’m creating them threw an import.

I have :

  • The instance of the configurable product
  • The instance of the simple product
  • The attribute value

This is what I have done which isn’t working apparently

This is what I’m doing in case i’m currently reading a “poids” attribute

case 'poids':
    $attribute = $this->eavAttributeRepository->get(
        ProductAttributeInterface::ENTITY_TYPE_CODE,'poids'
    );
    $usedAttributes = $configurable_product->getTypeInstance()->getUsedProductAttributeIds($configurable_product);
    $configurable_product->getTypeInstance()->setUsedProductAttributeIds(
        !in_array($attribute->getAttributeId(),$usedAttributes)
            ?array_merge($usedAttributes,[$attribute->getAttributeId()]):$usedAttributes,
        $configurable_product);
    $configurableAttributesData =
        $configurable_product->getTypeInstance()->getConfigurableAttributesAsArray($configurable_product);
    $configurable_product->setCanSaveConfigurableAttributes(true);
    $configurable_product->setConfigurableAttributesData($configurableAttributesData);


    $value = $attribute['value'];
    $configurableProductsData[$childProductId] = array(
        $k => array( //$k is an increment on the attribute i read 
            'label' => 'Poids', //attribute label
            'attribute_id' => $attribute->getAttributeId(),
            'value_index' => $this->poidsOptions->getLabelValue($value),
            'is_percent'    => 0,
            'pricing_value' => $childPrice,
        )
    );
    $configurable_product->setConfigurableProductsData($configurableProductsData);
    try{
        $this->productRepository->save($configurable_product);
    }catch(Exception $eI){
        var_dump($eI);
    }

No error printed, but no options neither appearing in my configurable

result

Any ideas ?