Skip to content

How to programmatically assign simple product to configurable on Magento 2.4

How do you programmatically assign simple product to configurable?
What I tried:

$configurableProduct = $this->objectManager->create('MagentoConfigurableProductModelProductTypeConfigurable');
    $configurableProduct->setUsedProductAttributes($product, array($product_jsa_type_attribute_id));
    $product->setNewVariationsAttributeSetId(4);
    $product->setAssociatedProductIds(array(
       $productObj->getIdBySku($ticketProduct['product_id']),
       $productObj->getIdBySku($couponProduct['product_id']),
    ));