Magento, Magento 2

How to Create Configurable Product Programmatically in Magento 2

How to Create Configurable Product Programmatically in Magento 2

In this tutorial, I will explain to how to create configurable product programmatically in Magento 2. Configurable product has simple product also. So, in this example i will create configurable product with simple product programmtically.

Configurable product is one of the best feature of Magento 2. When there are certain products that have multiple options, at that time configurable product is useful instead of create lots of simple products. You can save records and keep track on that each product’s inventory using configurable product.

So, Whenever we develop some functionality related to configurable product at that time, maybe we need to create large number of configurable product. But, if we are going to create from admin panel then, it will take a time to create large amount of configurable products.

You may also like this :

List of article to create product programmatically with different types :

Let’s now create a simple script for create configurable product. Create a configurable.php file at your Magento’s root folder and add this below code (It’s Working Code).

Configurable product with Simple Product Programmatically :

<?php
/**
 * Created By : Rohan Hapani
 */
use Magento\Framework\App\Bootstrap;

require __DIR__ . '/app/bootstrap.php';

$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$product = $objectManager->create('Magento\Catalog\Model\Product');

$product->setName('Configurable Product'); // Set Product Name
$product->setTypeId('configurable'); // Set Product Type Id
$product->setAttributeSetId(10); // Set Attribute Set ID
$product->setSku('configurable-product'); // Set SKU
$product->setStatus(1); // Set Status
$product->setWeight(5); // Set Weight
$product->setTaxClassId(2); // Set Tax Class Id
$product->setWebsiteIds([1]); // Set Website Ids
$product->setVisibility(4);
$product->setCategoryIds([3]); // Assign Category Ids
$product->setPrice(100); // Product Price
$product->setImage('/configurable/test.jpg'); // Image Path
$product->setSmallImage('/configurable/test.jpg'); // Small Image Path
$product->setThumbnail('/configurable/test.jpg'); // Thumbnail Image Path

$product->setStockData(
    [
        'use_config_manage_stock' => 0, // Use Config Settings Checkbox
        'manage_stock' => 1, // Manage Stock
        'is_in_stock' => 1, // Stock Availability
    ]
);
$size_attr_id = $product->getResource()->getAttribute('size')->getId();
$color_attr_id = $product->getResource()->getAttribute('color')->getId();
$product->getTypeInstance()->setUsedProductAttributeIds([$color_attr_id, $size_attr_id], $product);
$configurableAttributesData = $product->getTypeInstance()->getConfigurableAttributesAsArray($product);
$product->setCanSaveConfigurableAttributes(true);
$product->setConfigurableAttributesData($configurableAttributesData);
$configurableProductsData = [];
$product->setConfigurableProductsData($configurableProductsData);
try {
    $product->save();
} catch (Exception $ex) {
    echo '<pre>';
    print_r($ex->getMessage());
    exit;
}
$productId = $product->getId();
$associatedProductIds = [2044, 2045]; // Add Your Associated Product Ids.
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;
}

That’s it !!

In addition, You can set static details based on your requirements. If you want to create multiple products programmatically by the script at that time, you can use this script and customize code for multiple products create.

Now, execute this file at frontend side : {baseUrl}/configurable.php

I hope this blog will helpful for easily understand how to create a configurable product programmatically in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution.

Keep liking and sharing 🙂
Tagged ,