I want to create configurable product with simple product assign.
It should be working same as like create from admin.
I have tried so many tutorials and answers. But, it's not working.
Can anyone please help me?
Try this way...
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('memory_limit', '5G');
error_reporting(E_ALL);
use Magento\Framework\App\Bootstrap;
/**
* If the external file is in the root folder
*/
require __DIR__ . '/app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // instance of object manager
$category_id= array(67,68);
//$imglist = implode(',', $insert_csv['image']);
//echo $imagelist = array($imglist);
$configurable_product = $objectManager->create('\Magento\Catalog\Model\Product');
$configurable_product->setSku("test-sku"); // set sku
$configurable_product->setName("test-name"); // set name
$configurable_product->setAttributeSetId(4);
$configurable_product->setStatus(1);
$configurable_product->setTypeId('configurable');
$configurable_product->setVisibility(4);
$configurable_product->setPrice(0);
$configurable_product->setWebsiteIds(array(1)); // set website
$configurable_product->setCategoryIds($category_id); // set category
$configurable_product->setStockData(array(
'use_config_manage_stock' => 0, //'Use config settings' checkbox
'manage_stock' => 1, //manage stock
'is_in_stock' => 1, //Stock Availability
)
);
// super attribute
$size_attr_id = $configurable_product->getResource()->getAttribute('size')->getId();
$color_attr_id = $configurable_product->getResource()->getAttribute('color')->getId();
$configurable_product->getTypeInstance()->setUsedProductAttributeIds(array($color_attr_id, $size_attr_id), $configurable_product); //attribute ID of attribute 'size_general' in my store
$configurableAttributesData = $configurable_product->getTypeInstance()->getConfigurableAttributesAsArray($configurable_product);
$configurable_product->setCanSaveConfigurableAttributes(true);
$configurable_product->setConfigurableAttributesData($configurableAttributesData);
$configurableProductsData = array();
$configurable_product->setConfigurableProductsData($configurableProductsData);
try {
$configurable_product->save();
} catch (Exception $ex) {
echo '<pre>';
print_r($ex->getMessage());
exit;
}
$productId = $configurable_product->getId();
// assign simple product ids
$associatedProductIds = array(4609,4610,4611,4612);
try{
$configurable_product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId); // Load Configurable Product
$configurable_product->setAssociatedProductIds($associatedProductIds); // Setting Associated Products
$configurable_product->setCanSaveConfigurableAttributes(true);
$configurable_product->save();
} catch (Exception $e) {
echo "<pre>";
print_r($e->getMessage());
exit;
}