---
title: "How to Create Configurable Product Programmatically in Magento 2?"
url: "https://magecomp.com/blog/create-configurable-product-programmatically-in-magento-2/"
date: "2021-04-06T10:00:53+00:00"
modified: "2025-01-28T12:59:56+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "Magento Tutorials"
word_count: 496
reading_time: "3 min read"
summary: "Hello Magento Folks,"
description: "Creating products programmatically helps the developers to set up online stores. Learn how to create configurable products programmatically in Magento 2"
keywords: "Magento Tutorials"
language: "en"
schema_type: "Article"
related_posts:
  - title: "Magento 2: How to Remove Delete Attribute Option from Product Attribute"
    url: "https://magecomp.com/blog/remove-delete-attribute-option-from-product-attribute/"
  - title: "How to Setup Email ID for Contact us Form in Magento 2 [Updated]"
    url: "https://magecomp.com/blog/setup-email-id-for-contact-us-form-in-magento-2/"
  - title: "How to Install Magento 2 in localhost with Sample Data [Updated]"
    url: "https://magecomp.com/blog/how-to-install-magento-2-in-localhost-with-sample-data/"
---

# How to Create Configurable Product Programmatically in Magento 2?

_Published: April 6, 2021_  
_Author: Dhiren Vasoya_  

![How-to-Create-Configurable-Product-Programmatically-in-Magento-2](https://magecomp.com/blog/wp-content/uploads/2021/04/How-to-Create-Configurable-Product-Programmatically-in-Magento-2.png)

**Hello Magento Folks,**

Welcome to Create Product Programmatically in Magento 2 series where I will illustrate how to create configurable products programmatically in Magento 2.
If you are reading this tutorial then you are probably new to Magento. Therefore, firstly check out the types of products in my previous blog [**Magento 2 Product Types: All You Need to Know Guide**](https://magecomp.com/blog/magento-2-product-types/). Basically, creating products programmatically helps the developers to set up online stores effortlessly. When there is a requirement for different colors and sizes for the same product then in Magento you will need to create a configurable product.
**Mainly we can create 6 types of products Programmatically in Magento 2:**

- [**Simple Product**](https://magecomp.com/blog/create-simple-product-programmatically-in-magento-2/)
- **[Configurable Product](https://magecomp.com/blog/create-simple-product-programmatically-in-magento-2/)**
- Grouped Product- Virtual Product- Bundle Product- Downloadable Product
## Steps to create configurable product programmatically in Magento 2:
**Step 1:** Firstly create a **configurable_product.php** file in the Magento root directory and add the below-given code
```
<?php
use MagentoFrameworkAppInterface;
try {
    require_once __DIR__ . '/app/bootstrap.php';
} catch (Exception $e) {
    echo 'Autoload error: ' . $e->getMessage();
    exit(1);
}
try {
    $bootstrap = MagentoFrameworkAppBootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();
    $appState = $objectManager->get('MagentoFrameworkAppState');
    $appState->setAreaCode('frontend');

    $configurableproduct = $objectManager->create('MagentoCatalogModelProduct');
    $configurableproduct->setSku("configurable_sku"); // set Your SKU
    $configurableproduct->setName("Configurable Name"); // set Your Configurable Name
    $configurableproduct->setAttributeSetId(4); // set attribute id
    $configurableproduct->setStatus(1); // status enabled/disabled 1/0
    $configurableproduct->setTypeId('configurable'); // type of product (simple/virtual/downloadable/configurable)
    $configurableproduct->setVisibility(4);  // visibility of product (Not Visible Individually (1) / Catalog (2)/ Search (3)/ Catalog, Search(4))
    $configurableproduct->setPrice(0);
    $configurableproduct->setTaxClassId(0); // Tax class ID
    $configurableproduct->setWebsiteIds(array(1)); // set website Id
    $category_id = array(2, 3);
    $configurableproduct->setCategoryIds($category_id);
    $configurableproduct->setStockData(array(
            'use_config_manage_stock' => 0,
            'manage_stock' => 1,
            'is_in_stock' => 1,
        )
    );
    $configurableattributesdata = $configurableproduct->getTypeInstance()->getConfigurableAttributesAsArray($configurableproduct);
    $configurableproduct->setCanSaveConfigurableAttributes(true);
    $configurableproduct->setConfigurableAttributesData($configurableattributesdata);
    $configurableproductsdata = array();
    $configurableproduct->setConfigurableProductsData($configurableproductsdata);
    try {
        $configurableproduct->save();
    } catch (Exception $ex) {
        echo '';
        print_r($ex->getMessage());
        exit(1);
    }
    $product_id = $configurableproduct->getId();
    $associatedproductids = array(1,9);
    try {
        $configurableproduct_load = $objectManager->create('MagentoCatalogModelProduct')->load($product_id);
        $configurableproduct_load->setAssociatedProductIds($associatedproductids);
        $configurableproduct_load->setCanSaveConfigurableAttributes(true);
        $configurableproduct_load->save();
        echo "configurable product save successfully";
    } catch (Exception $e) {
        echo "";
        print_r($e->getMessage());
        exit(1);
    }
}catch (Exception $e) {
    echo "";
    print_r($e->getMessage());
    exit(1);
}
```

**Step 2:** After the above step you will need to run the below-given URL to create a configurable product.
```
https://yourdomain.com/configurable_product.php
```

## Wrapping Up:
Hopefully, all are able to create configurable products programmatically in Magento 2 using the above code. As customary of Magento, clear cache and reindex to display output on the frontend of your Magento 2 store. Let me know implementation concerns in the comment section below. You can also take our certified developer’s help using the [**Magento Multi-Store & Website Setup Service.** ](https://magecomp.com/magento-mutistore-website-setup.html)

Share with your friends via social media.
Happy Reading!


---

_View the original post at: [https://magecomp.com/blog/create-configurable-product-programmatically-in-magento-2/](https://magecomp.com/blog/create-configurable-product-programmatically-in-magento-2/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-07-21 23:25:47 UTC_  
