title: Programmatically (manually) creating Configurable Magento 2 Product
body: <p><strong>Programmatically (manually) creating Configurable Magento 2 Product :</strong></p>
  
  <p>The Product are generated correctly, but I am facing issue with, not generating product <strong>Configuration</strong> Details. </p>
  
  <p>This <strong>Configuration</strong> details will help to generate the relation between Configurable product and associate Child Products, these part not working while programmatically create the Product.</p>
  
  <p>Please suggest solution for this issue.</p>
  
  <p>Please find my below code:</p>
  
  <pre><code>&lt;?php 
  ini_set("display_errors",1);
  $magentoDirPath=dirname(dirname(__FILE__)); 
  use \Magento\Framework\App\Bootstrap;
  include($magentoDirPath.'/app/bootstrap.php');  //   File Path 
  
  // add bootstrap
  $bootstraps = Bootstrap::create(BP, $_SERVER);
  $object_Manager = $bootstraps-&gt;getObjectManager();
  
  $app_state = $object_Manager-&gt;get('\Magento\Framework\App\State');
  $app_state-&gt;setAreaCode('frontend');
  
  //simple product
  $simple_product = $object_Manager-&gt;create('\Magento\Catalog\Model\Product');
  $simple_product-&gt;setSku('test-simple-randdd');
  $simple_product-&gt;setName('test name simple');
  $simple_product-&gt;setAttributeSetId(4);
  $simple_product-&gt;setColor(4); // value id of S size
  $simple_product-&gt;setStatus(1);
  $simple_product-&gt;setTypeId('simple');
  $simple_product-&gt;setPrice(10);
  $simple_product-&gt;setWebsiteIds(array(1));
  $simple_product-&gt;setCategoryIds(array(4,5));
  $simple_product-&gt;setStockData(array(
      'use_config_manage_stock' =&gt; 0, //'Use config settings' checkbox
      'manage_stock' =&gt; 1, //manage stock
      'min_sale_qty' =&gt; 1, //Minimum Qty Allowed in Shopping Cart
      'max_sale_qty' =&gt; 2, //Maximum Qty Allowed in Shopping Cart
      'is_in_stock' =&gt; 1, //Stock Availability
      'qty' =&gt; 100 //qty
      )
  );
  
  $simple_product-&gt;save();
  
  $simple_product_id = $simple_product-&gt;getId();
  echo "simple product id: ".$simple_product_id."\n";
  
  //configurable product
  $configurable_product = $object_Manager-&gt;create('\Magento\Catalog\Model\Product');
  $configurable_product-&gt;setSku('test-configurable');
  $configurable_product-&gt;setName('test name configurable');
  $configurable_product-&gt;setAttributeSetId(4);
  $configurable_product-&gt;setColor(4); // value id of S size
  $configurable_product-&gt;setStatus(1);
  $configurable_product-&gt;setTypeId('configurable');
  $configurable_product-&gt;setPrice(11);
  $configurable_product-&gt;setWebsiteIds(array(1));
  $configurable_product-&gt;setCategoryIds(array(4,5));
  $configurable_product-&gt;setStockData(array(
     'use_config_manage_stock' =&gt; 0, //'Use config settings' checkbox
      'manage_stock' =&gt; 1, //manage stock
      'min_sale_qty' =&gt; 1, //Minimum Qty Allowed in Shopping Cart
      'max_sale_qty' =&gt; 2, //Maximum Qty Allowed in Shopping Cart
      'is_in_stock' =&gt; 1, //Stock Availability
      'qty' =&gt; 100 //qty
      )
  );
  $configurable_product-&gt;getTypeInstance()-&gt;setUsedProductAttributeIds(array(93),$configurable_product); //attribute ID of attribute 'size_general' in my store
  $configurableAttributesData = $configurable_product-&gt;getTypeInstance()-&gt;getConfigurableAttributesAsArray($configurable_product);
  
  $configurable_product-&gt;setCanSaveConfigurableAttributes(true);
  $configurable_product-&gt;setConfigurableAttributesData($configurableAttributesData);
  
  
  
  $configurableProductsData = array();
  $configurableProductsData[$simple_product_id] = array( 
      '0' =&gt; array(
          'label' =&gt; 'Color', //attribute label
          'attribute_id' =&gt; '93', //attribute ID of attribute 'size_general' in my store
          'value_index' =&gt; '4', //value of 'S' index of the attribute 'size_general'
          'is_percent'    =&gt; 0,
          'pricing_value' =&gt; '10',
      )
  );
  
  // 1 - Static ID of Product
  
  $configurableProductsData['1'] = array(             
      '0' =&gt; array(
          'label' =&gt; 'Color', //attribute label
          'attribute_id' =&gt; '93', //attribute ID of attribute 'size_general' in my store
          'value_index' =&gt; '5', //value of 'S' index of the attribute 'size_general'
          'is_percent'    =&gt; 0,
          'pricing_value' =&gt; '10',
      )
  );
  
  // 91 - Static ID of Product
  $configurableProductsData['91'] = array( 
      '0' =&gt; array(
          'label' =&gt; 'Color', //attribute label
          'attribute_id' =&gt; '93', //attribute ID of attribute 'size_general' in my store
          'value_index' =&gt; '6', //value of 'S' index of the attribute 'size_general'
          'is_percent'    =&gt; 0,
          'pricing_value' =&gt; '10', ini_set("display_errors",1);
  
          $magentoDirPath=dirname(dirname(__FILE__)); 
          use \Magento\Framework\App\Bootstrap;
          include($magentoDirPath.'/app/bootstrap.php');  //   File Path 
  
  
  
          // add bootstrap
          $bootstraps = Bootstrap::create(BP, $_SERVER);
          $object_Manager = $bootstraps-&gt;getObjectManager();
  
          $app_state = $object_Manager-&gt;get('\Magento\Framework\App\State');
          $app_state-&gt;setAreaCode('frontend');
  
  
          //simple product
          $simple_product = $object_Manager-&gt;create('\Magento\Catalog\Model\Product');
          $simple_product-&gt;setSku('test-simple-randdd');
          $simple_product-&gt;setName('test name simple');
          $simple_product-&gt;setAttributeSetId(4);
          $simple_product-&gt;setColor(4); // value id of S size
          $simple_product-&gt;setStatus(1);
          $simple_product-&gt;setTypeId('simple');
          $simple_product-&gt;setPrice(10);
          $simple_product-&gt;setWebsiteIds(array(1));
          $simple_product-&gt;setCategoryIds(array(4,5));
          $simple_product-&gt;setStockData(array(
              'use_config_manage_stock' =&gt; 0, //'Use config settings' checkbox
              'manage_stock' =&gt; 1, //manage stock
              'min_sale_qty' =&gt; 1, //Minimum Qty Allowed in Shopping Cart
              'max_sale_qty' =&gt; 2, //Maximum Qty Allowed in Shopping Cart
              'is_in_stock' =&gt; 1, //Stock Availability
              'qty' =&gt; 100 //qty
              )
          );
  
          $simple_product-&gt;save();
  
          $simple_product_id = $simple_product-&gt;getId();
          echo "simple product id: ".$simple_product_id."\n";
  
          //configurable product
          $configurable_product = $object_Manager-&gt;create('\Magento\Catalog\Model\Product');
          $configurable_product-&gt;setSku('test-configurable');
          $configurable_product-&gt;setName('test name configurable');
          $configurable_product-&gt;setAttributeSetId(4);
          $configurable_product-&gt;setColor(4); // value id of S size
          $configurable_product-&gt;setStatus(1);
          $configurable_product-&gt;setTypeId('configurable');
          $configurable_product-&gt;setPrice(11);
          $configurable_product-&gt;setWebsiteIds(array(1));
          $configurable_product-&gt;setCategoryIds(array(4,5));
          $configurable_product-&gt;setStockData(array(
             'use_config_manage_stock' =&gt; 0, //'Use config settings' checkbox
              'manage_stock' =&gt; 1, //manage stock
              'min_sale_qty' =&gt; 1, //Minimum Qty Allowed in Shopping Cart
              'max_sale_qty' =&gt; 2, //Maximum Qty Allowed in Shopping Cart
              'is_in_stock' =&gt; 1, //Stock Availability
              'qty' =&gt; 100 //qty
              )
          );
  
          $configurable_product-&gt;getTypeInstance()-&gt;setUsedProductAttributeIds(array(93),$configurable_product); //attribute ID of attribute 'size_general' in my store
          $configurableAttributesData = $configurable_product-&gt;getTypeInstance()-&gt;getConfigurableAttributesAsArray($configurable_product);
  
          $configurable_product-&gt;setCanSaveConfigurableAttributes(true);
          $configurable_product-&gt;setConfigurableAttributesData($configurableAttributesData);
  
          $configurableProductsData = array();
          $configurableProductsData[$simple_product_id] = array( 
              '0' =&gt; array(
                  'label' =&gt; 'Color', //attribute label
                  'attribute_id' =&gt; '93', //attribute ID of attribute 'size_general' in my store
                  'value_index' =&gt; '4', //value of 'S' index of the attribute 'size_general'
                  'is_percent'    =&gt; 0,
                  'pricing_value' =&gt; '10',
              )
          );
  
          // 1 - Static ID of Product
  
          $configurableProductsData['1'] = array(             
              '0' =&gt; array(
                  'label' =&gt; 'Color', //attribute label
                  'attribute_id' =&gt; '93', //attribute ID of attribute 'size_general' in my store
                  'value_index' =&gt; '5', //value of 'S' index of the attribute 'size_general'
                  'is_percent'    =&gt; 0,
                  'pricing_value' =&gt; '10',
              )
          );
  
          // 91 - Static ID of Product
             $configurableProductsData['91'] = array( 
              '0' =&gt; array(
                  'label' =&gt; 'Color', //attribute label
                  'attribute_id' =&gt; '93', //attribute ID of attribute 'size_general' in my store
                  'value_index' =&gt; '6', //value of 'S' index of the attribute 'size_general'
                  'is_percent'    =&gt; 0,
                  'pricing_value' =&gt; '10',
              )
          );
  
          $configurable_product-&gt;setConfigurableProductsData($configurableProductsData);
  
          $associatedProductIds =array($simple_product_id,'1','91'); 
          $configurable_product-&gt;setCanSaveConfigurableAttributes(true);
          $configurable_product-&gt;getTypeInstance()-&gt;setUsedProductAttributeIds(array(93), $configurable_product);
          $configurable_product-&gt;setNewVariationsAttributeSetId($configurable_product-&gt;getAttributeSetId());
          $configurable_product-&gt;setAssociatedProductIds($associatedProductIds);
          $configurable_product-&gt;setConfigurableAttributesData($configurableAttributesData);
  
  
          $configurable_product-&gt;save();
          $configProductId = $configurable_product-&gt;getId();
  
          echo "Product id: ".$configurable_product-&gt;getId()."\n";
  
          exit;
      )
  );
  
  $configurable_product-&gt;setConfigurableProductsData($configurableProductsData);
  
  $associatedProductIds =array($simple_product_id,'1','91'); 
  $configurable_product-&gt;setCanSaveConfigurableAttributes(true);
  $configurable_product-&gt;getTypeInstance()-&gt;setUsedProductAttributeIds(array(93), $configurable_product);
  $configurable_product-&gt;setNewVariationsAttributeSetId($configurable_product-&gt;getAttributeSetId());
  $configurable_product-&gt;setAssociatedProductIds($associatedProductIds);
  $configurable_product-&gt;setConfigurableAttributesData($configurableAttributesData);
  
  $configurable_product-&gt;save();
  $configProductId = $configurable_product-&gt;getId();
  
  echo "Product id: ".$configurable_product-&gt;getId()."\n";
  
  exit;
  ?&gt;
  </code></pre>