How can I associate a Magento 2 simple product to a Magento 2 configurable product programmatically
You can call “setAssociatedProductIds” to associate some Magento 2 products to a Magento 2 configurable product programmatically.
$associatedProductIds = [2,3,4,5];//Simple Product ids array $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $product = $objectManager->create('Magento\Catalog\Model\Product')->load(1); // Load Configurable Product $product->setAssociatedProductIds($associatedProductIds); $product->save();