How To Associate Magento 2 Simple Product To The Configurable Product Programmatically

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();
1 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments