How to create a configurable product programmatically?

Hi Sandeep,

Can you share the programmatically create configurable products script.

Note:
1- If you want to create configurable product then you have to assign
associated product to current product otherwise it will create simple
product. See here how to add associated products.
2- If you do not set weight, it will create simple product.

code to assign product to configurable product :slight_smile:
$productId = 12; // Configurable Product Id$objectManager = \Magento\Framework\App\ObjectManager::getInstance();$product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId); // Load Configurable Product$attributeModel = $objectManager->create('Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute');$position = 0;$attributes = array(134, 135); // Super Attribute Ids Used To Create Configurable Product$associatedProductIds = array(2,4,5,6); //Product Ids Of Associated Productsforeach ($attributes as $attributeId) { $data = array('attribute_id' => $attributeId, 'product_id' => $productId, 'position' => $position); $position++; $attributeModel->setData($data)->save();}$product->setTypeId("configurable"); // Setting Product Type As Configurable$product->setAffectConfigurableProductAttributes(4);$objectManager->create('Magento\ConfigurableProduct\Model\Product\Type\Configurable')->setUsedProductAttributeIds($attributes, $product);$product->setNewVariationsAttributeSetId(4); // Setting Attribute Set Id$product->setAssociatedProductIds($associatedProductIds);// Setting Associated Products$product->setCanSaveConfigurableAttributes(true);$product->save();

For simple Product :slight_smile:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // instance of object manager
$product = $objectManager->create(’\Magento\Catalog\Model\Product’);
$product->setSku(‘sku’); // Set your sku here
$product->setName(‘Sample Product’); // Name of Product
$product->setAttributeSetId(4); // Attribute set id
$product->setStatus(1); // Status on product enabled/ disabled 1/0
$product->setWeight(10); // weight of product
$product->setVisibility(4); // visibilty of product (catalog / search / catalog, search / Not visible individually)
$product->setTaxClassId(0); // Tax class id
$product->setTypeId(‘simple’); // type of product (simple/virtual/downloadable/configurable)
$product->setPrice(100); // price of product
$product->setStockData(
array(
‘use_config_manage_stock’ => 0,
‘manage_stock’ => 1,
‘is_in_stock’ => 1,
‘qty’ => 999999999
)
);
$product->save();

// Adding Image to product
$imagePath = “sample.jpg”; // path of the image
$product->addImageToMediaGallery($imagePath, array(‘image’, ‘small_image’, ‘thumbnail’), false, false);
$product->save();

// Adding Custom option to product
$options = array(
array(
“sort_order” => 1,
“title” => “Custom Option 1”,
“price_type” => “fixed”,
“price” => “10”,
“type” => “field”,
“is_require” => 0
),
array(
“sort_order” => 2,
“title” => “Custom Option 2”,
“price_type” => “fixed”,
“price” => “20”,
“type” => “field”,
“is_require” => 0
)
);
foreach ($options as $arrayOption) {
$product->setHasOptions(1);
$product->getResource()->save($product);
$option = $objectManager->create(’\Magento\Catalog\Model\Product\Option’)
->setProductId($product->getId())
->setStoreId($product->getStoreId())
->addData($arrayOption);
$option->save();
$product->addOption($option);
}

Thank you…!!!