How to import configurable product in magento 2 using CSV?

You can import configurable products in Magento 2 by CSV from the admin panel.

Select CSV from the backend,
Go To System -> Data Transfer -> Import
From Entity Type Select Products.
Select Add/Update from Import Behavior settings.

For Import Configurable product using csv, first verify its simple product exist or not in catalog, If child product is not available in catalog, You must create first its child product or Assigned all the child product in CSV before configurable product row.

You are familiar with simple product import. if you don’t know how to import simple product please refer link. How to import simple products in Magento 2?

From simple product to configurable product main difference in column are configurable_variations and configurable_variation_labels
Both column is must required to import configurable product.

configurable_variation_labels => size=Size,color=Color
Super attribute code if the configurable product made with multiple attributes keep value with comma-separated.
Example, where size, color(lowercase) is attributed code and Size and Color(attribute label)

configurable_variations => It contains the pair of all the child product SKU, super attribute value separated with PIPE(|) operator.

Example,
sku=Test_Sku,size=S,color=Red|sku=Test Simple 2,size=M,color=Blue|sku=Test Simple 3,size=L,color=White|sku=Test Simple 4,size=XL,color=Yellow

Where sku=Test_Sku, size=S, color=Red (Test_Sku is the sku and color and size value) for second product we must use pipe(|) operator and set value of second simple product and so on.

Don’t forget to run  indexer command,
php bin/magento indexer:reindex

Download the Configurable Product Import Sheet.

Check Import Additional Attributes Value Using CSV.

Magento 2 How to get child item id of configurable product by super attribute details?

Let’s assume you know configurable product id and used child product super attribute details.
Example from Magento Sample data configurable product name Chaz Kangeroo Hoodie,  Configurable product id is 67.
Super attribute Array details,

[super_attribute] => Array
(
    [93] => 49
    [254] => 167
)

Using the bove details how we can get details of child item.

Refer below code snippet for Get child item id of a Configurable product by child’s Attribute details.

<?php
public function __construct(
    \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
    \Magento\ConfigurableProduct\Model\Product\Type\Configurable $configurable
) {
    $this->productRepository = $productRepository;
    $this->configurable = $configurable;
}
/* Pass Configurable product id as $configId 
 * Pass array of super attribute of child item
 */
public function getChildFromProductAttribute($configId,$superAttribute) {
	$_configProduct = $this->productRepository->getById($configId);
	$usedChild = $this->configurable->getProductByAttributes($superAttribute ,$_configProduct);
	$childProductId = $usedChild->getId();
	return $childProductId;
}

Call function,

$configId = 67; //Configurable Product id
$superAttribute = Array(93 => 49,254 => 167); //childs super attribute details
$childId = $this->getChildFromProductAttribute($configId,$superAttribute);

Result is 52(Product name Chaz Kangeroo Hoodie-XS-Black). Using above way we got the child item data.

How to get configurable product’s used super_attribute details programmatically in Magento 2?

We can get the details of the used super attribute value for the configurable product. You will get the attribute value used in the configurable product using the below example.

Let’s assume a configurable product generated with color and size attributes. But how we can get details of super attribute value using a programmatic way that product is made using color or size or color and size or any other attributes combination.

You can retrieve the Child Product Used Super attribute details for the configurable product.

We can get Dynamically Configurable Product’s used Super attribute details by below code snippet.

<?php
namespace Rbj\ConfigProduct\Block;

class SuperAttribute
{
    public function __construct(
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
    ) {
        $this->productRepository = $productRepository;
    }

    /**
     * get Super Attribute details by configurable product id
     */
    public function getSuperAttributeData($productId);
    {
        /** @var \Magento\Catalog\Model\Product $product */
        $product = $this->productRepository->getById($id);
        if ($product->getTypeId() != \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE) {
            return [];
        }

        /** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable $productTypeInstance */
        $productTypeInstance = $product->getTypeInstance();
        $productTypeInstance->setStoreFilter($product->getStoreId(), $product);

        $attributes = $productTypeInstance->getConfigurableAttributes($product);
        $superAttributeList = [];
        foreach($attributes as $_attribute){
            $attributeCode = $_attribute->getProductAttribute()->getAttributeCode();;
            $superAttributeList[$_attribute->getAttributeId()] = $attributeCode;
        }
        return $superAttributeList;
    }

Call function from a PHP class file,

$productId = 67;
$superAttribute = $this->getSuperAttributeData($productId);
echo "<pre>";print_r($superAttribute);

The result will be,

Array
(
   [93] => color
   [254] => size
)

Where 93,254 are attribute id and color, size is attribute code. Based on output we can say a configurable product is made using color and size super attribute.