0

How to create Configurable products with associate simple products pragmatically by CSV data by root file.

1

First you have to get csv data like below

$path = "Product.csv";
$fp = fopen($path, 'r') or die("can't open file");
$row = 0;

$i = 0;
while ($column = fgetcsv($fp, 10000)) {
    if ($i == 0) {
        $i++;
        continue;
    }

/* here are code for create simple product  / configurable product */
/* first create all simple product */
/* then create configratable product which assign its assosiated product */


}

Create Simple product as per your csv requirement

<?php

use Magento\Framework\App\Bootstrap;

require 'app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('\Magento\Framework\App\State');
$state->setAreaCode('frontend');

$product = $objectManager->create('Magento\Catalog\Model\Product');

try {
    $product->setName('Test Product');
    $product->setTypeId('simple');
    $product->setAttributeSetId(4);
    $product->setSku('test-SKU');
    $product->setWebsiteIds(array(1));
    $product->setVisibility(4);
    $product->setPrice(array(1));
    $product->setImage('/testimg/test.jpg');
    $product->setSmallImage('/testimg/test.jpg');
    $product->setThumbnail('/testimg/test.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' => 100
        )
    );

    $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);
    }
} catch (\Exception $e) {
    echo $e->getMessage();
}

* Then Create configurable product with assign its assosiated *

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$configurable_product = $objectManager->create('\Magento\Catalog\Model\Product');

$configurable_product->setSku('CONFIG_SKU'); // set sku
$configurable_product->setName('CONFIG PRODUCT NAME'); // set name
$configurable_product->setAttributeSetId(4);
$configurable_product->setStatus(1);
$configurable_product->setTypeId('configurable');
$configurable_product->setPrice(0);
$configurable_product->setWebsiteIds(array(1)); // set website
$configurable_product->setCategoryIds(array(2)); // 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('product_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(12,13,14);

try{
$configurable_product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId); // Load Configurable Product
    $configurable_product->setAssociatedProductIds($assocIds); // Setting Associated Products
    $configurable_product->setCanSaveConfigurableAttributes(true);
    $configurable_product->save();
} catch (Exception $e) {
    echo "<pre>";
    print_r($e->getMessage());
    exit;
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.