Creating configurable products you just` create simple products with some configurable options for customers to choose from. So, to create a configurable product programmatically you should start by creating a simple product and then assigning some features to it.
Follow these steps to create a configurable product in Magento 2 programmatically:
1. Create a simple product.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product');
$sku = 'sku';
$product->setSku($sku);
$product->setName('Simple Product');
$product->setAttributeSetId(4);
$product->setStatus(1);
$product->setWeight(1);
$product->setVisibility(4);
$product->setWebsiteIds(array(1));
$product->setTaxClassId(0);
$product->setTypeId('simple');
$product->setPrice(100);
$product->setStockData(
array(
'use_config_manage_stock' => 0,
'manage_stock' => 1,
'min_sale_qty' => 1,
'max_sale_qty' => 2,
'is_in_stock' => 1,
'qty' => 1000
)
);
$product->save();
$categoryIds = array('2','3');
$category = $objectManager->get('Magento\Catalog\Api\CategoryLinkManagementInterface');
$category->assignProductToCategories($sku, $categoryIds);
if ($product->getId())
{
echo "Product Created";
}
2. Create a configurable product and assign a simple one to it.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create(Magento\Catalog\Model\Product::class);
$sku = 'sku11';
$product->setSku($sku);
$product->setName('Grouped Product31');
$product->setWeight(1);
$product->setPrice(100);
$product->setDescription('description');
$product->setAttributeSetId(4);
$product->setCategoryIds(array(35));
$product->setStatus(1);
$product->setVisibility(4);
$product->setTaxClassId(1);
$product->setTypeId('grouped');
$product->setStoreId(1);
$product->setWebsiteIds(array(1));
$product->setVisibility(4);
$product->setImage('/groupedproduct/image.jpg');
$product->setSmallImage('/groupedproduct/image.jpg');
$product->setThumbnail('/groupedproduct/image.jpg');
$product->setStockData(array(
'use_config_manage_stock' => 0,
'manage_stock' => 1,
'min_sale_qty' => 1,
'max_sale_qty' => 2,
'is_in_stock' => 1,
'qty' => 1000
)
);
$product->save();
$childrenIds = [2, 3, 4];
$associated = [];
$position = 0;
foreach ($childrenIds as $productId)
{
$position++;
$linkedProduct = $objectManager->get('\Magento\Catalog\Api\ProductRepositoryInterface')->getById($productId);
$productLink = $objectManager->create('\Magento\Catalog\Api\Data\ProductLinkInterface');
$productLink->setSku($product->getSku())
->setLinkType('associated')
->setLinkedProductSku($linkedProduct->getSku())
->setLinkedProductType($linkedProduct->getTypeId())
->setPosition($position)
->getExtensionAttributes()
->setQty(1);
$associated[] = $productLink;
}
$product->setProductLinks($associated);
$product->save();
$categoryIds = array('2','3');
$category = $objectManager->get('Magento\Catalog\Api\CategoryLinkManagementInterface');
$category->assignProductToCategories($sku, $categoryIds);
if ($product->getId())
{
echo "Product Created";
}
Was it hard, Coders?
Now you should go and check the configurable product on the storefront.
If you don't want to work the code you can simply create configurable product in the admin panel.
And just in case you are interested in other types of products Magento allows you to manage, check the Magento Product Types Guide.