By using this code snippet, you can create the configurable product. Make changes accordingly yours
<?php
$productId= 03; // Configurable Product Id
$objectManager= \Magento\Framework\App\ObjectManager::getInstance();
$product= $objectManager->create('Magento\Catalog\Model\Product')->load($productId); // Load Configurable Product
$attributeModel= $objectManager->create('Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute');
$position= 0;
$attributes= array(134, 135); // Super Attribute Ids Used To Create Configurable Product
$associatedProductIds= array(1,2); //Product Ids Of Associated Products
foreach($attributesas$attributeId) {
$data = array('attribute_id'=> $attributeId, 'product_id'=> $productId, 'position'=> $position);
$position++;
$attributeModel->setData($data)->save();
}
$product->setTypeId("configurable"); // Setting Product Type As Configurable
$product->setAffectConfigurableProductAttributes(4);
$objectManager->create('Magento\ConfigurableProduct\Model\Product\Type\Configurable')->setUsedProductAttributeIds($attributes, $product);
$product->setNewVariationsAttributeSetId(4); // Setting Attribute Set Id
$product->setAssociatedProductIds($associatedProductIds); // Setting Associated Products
$product->setCanSaveConfigurableAttributes(true);
$product->save();
?>