Configurable products definitely stand out in the list of other Magento 2 product types. They come in handy when you want to provide your customers with multiple product options.
By default, Magento 2 doesn't allow you to assign the existing simple products to the existing configurable products from the admin panel. The only option is to create a so-called placeholder in the configuration and then add the product you need instead of it.
That kind of solution is not really helpful, since it requires too many unnecessary steps. That is why a programmatic way to perform this task might of more use.
So, to add a simple product to a configurable product in Magento 2 programmatically, refer to the method below.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $simple_product = $objectManager->create(\Magento\Catalog\Model\Product::class); $simple_product->setSku('test-simple'); $simple_product->setName('test name simple'); $simple_product->setAttributeSetId(10); $simple_product->setSize_general(193); // value id of S size $simple_product->setStatus(1); $simple_product->setTypeId('simple'); $simple_product->setPrice(10); $simple_product->setSize(91); $simple_product->setColor(49); $simple_product->setWebsiteIds(array(1)); $simple_product->setCategoryIds(array(31)); $simple_product->setStockData(array( 'use_config_manage_stock' => 0, //'Use config settings' checkbox 'manage_stock' => 1, //manage stock 'min_sale_qty' => 1, //Minimum Qty Allowed in Shopping Cart 'max_sale_qty' => 2, //Maximum Qty Allowed in Shopping Cart 'is_in_stock' => 1, //Stock Availability 'qty' => 100 //qty ) ); $simple_product->save(); $simple_product_id = $simple_product->getId(); echo "simple product id: ".$simple_product_id."\n"; //configurable product $configurable_product = $objectManager->create(\Magento\Catalog\Model\Product::class); $configurable_product->setSku('test-configurable'); $configurable_product->setName('test name configurable'); $configurable_product->setAttributeSetId(4); $configurable_product->setStatus(1); $configurable_product->setTypeId('configurable'); $configurable_product->setPrice(11); $configurable_product->setWebsiteIds(array(1)); $configurable_product->setCategoryIds(array(31)); $configurable_product->setStockData(array( 'use_config_manage_stock' => 0, //'Use config settings' checkbox 'manage_stock' => 1, //manage stock 'is_in_stock' => 1, //Stock Availability ) ); $size_attr_id = $configurable_product->getResource()->getAttribute('size')->getId(); $color_attr_id = $configurable_product->getResource()->getAttribute('color')->getId(); $configurable_product->getTypeInstance()->setUsedProductAttributeIds([$color_attr_id, $size_attr_id], $configurable_product); $configurableAttributesData = $configurable_product->getTypeInstance()->getConfigurableAttributesAsArray($configurable_product); $configurable_product->setConfigurableAttributesData($configurableAttributesData); $configurableProductsData = []; $configurable_product->setConfigurableProductsData($configurableProductsData); $productId = $configurable_product->getId(); $associatedProductIds = [$simple_product_id]; // Add Your Associated Product Ids. $configurable_product->setAssociatedProductIds($associatedProductIds); // Setting Associated Products $configurable_product->setCanSaveConfigurableAttributes(true); $configurable_product->save(); echo "configurable product id: ".$configurable_product->getId()."\n";
There you go! Hopefully, this solution will help you to eliminate the struggles with product management.
Once you learn how to add a simple product to a configurable one, you might also want to know how to get current product in Magento.