0

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. These options are properly configured, they appear in the admin if I want to add them from the admin

options

But I'm creating them through 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?

1

1 Answer 1

0

Each configurable product must have an attribut set This attribut set contain attributs

Create or modify the product attribut set

Stores >> Attritubes >> Attribut set

Drag and drop the attribut

From your configurable product select the attribut set, then link your simple product to the configurable

enter image description here

I hope I understood the request.

1
  • I can link them threw the admin already this isn't an issue; what is not working is when I try to do it programmaticaly
    – Claims
    Commented Dec 2, 2021 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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