In magento 2 I had created 3 product via my code. Product type is configurable, simple, simple. so now don't know set two simple product into configuration product. thank for help!
1 Answer
You can associate the simple products by loading the Configurable product.
Inject the below Class to Construct Method \Magento\Catalog\Model\ProductRepository $productRepository,
So, $this->_productRepository = $productRepository;
Assuming the id of the configurable product as '3' and the simple as ,'1' and '2' respectively.
$associatedProductIds = [2,3];//Simple Product ids array
$configurableProduct = $this->_productRepository->getById(3);
$configurableProduct->setAssociatedProductIds($associatedProductIds);
$configurableProduct->save();
Hope this helps