How to Create Configurable Product with Magento 2 API

Magento 2 supports six Default types of products, including Simple, Grouped, Virtual, Bundle, Configurable, Bundle, and Downloadable products.

In this article, I will instruct you to create a configurable product with Magento 2 API through the below steps!

Before that, you might need some information about configurable products to understand fully about this product type to use it more effectively in your Magento 2 stores.

3 Steps to create configurable products in Magento 2:

Overview of Configurable Products in Magento 2

A configurable product comes with drop-down lists of options for each variation. Each option is a separate simple product with a unique SKU, so it is possible to track inventory for each product variation. The configurable product with multiple options is the proper choice to accelerate your business.

Step 1: Plan The Product

Assuming that we create a gray t-shirt that offers three sizes (small, medium, and large). The size of the T-shirt will be the configurable aspect of this product.

Define Product Characteristics

The following table displays the attributes of the men t-shirt we are creating. These items are among those displayed on the New Product page in Admin when you select the Top attribute.

Characteristic Description
Attribute Set Top
Product Name Dime Tee
SKU MS-Dime
Price 30.00
Tax Class Taxable Goods
Weight 0.6
Categories Men, Tops, Tees
Visibility Catalog, Search
Material LumaTech
Pattern Graphic Print
Color Gray
Size Configurable in small, medium, or large
Description The Dime Tee is soft, comfortable and durable. You will love the way you look in this tailored tee shirt.

The online store typically gives the product name, SKU, price, weight, and description. The system defines other attributes.

Find The System-Defined Values

To find the values needed to create the configurable product, we have to make several calls.

Get The Attribute Set ID

The sample data offers multiple attribute set, including Default, Top, and Bottom. To assign the Top attribute set to the product, we have to know the corresponding attribute_set_id.

Use the below call to find the attribute set named Top.

  • Endpoint
GET <host>/rest/<store_code>/V1/eav/attribute-sets/list?
searchCriteria[filter_groups][0][filters][0][field]=attribute_set_name&
searchCriteria[filter_groups][0][filters][0][value]=Top&
searchCriteria[filter_groups][0][filters][0][condition_type]=eq
  • Response

The attribute_set_id for the Top attribute set is 9.

{
    "items": [
        {
            "attribute_set_id": 9,
            "attribute_set_name": "Top",
            "sort_order": 0,
            "entity_type_id": 4
        }
    ],
    "search_criteria": {
        "filter_groups": [
            {
                "filters": [
                    {
                        "field": "attribute_set_name",
                        "value": "Top",
                        "condition_type": "eq"
                    }
                ]
            }
        ]
    },
    "total_count": 1
}

Find the list of attributes defined in an attribute searchCriteria

To return information about the attributes defined in the Top attribute set. please use the GET V1/products/attribute-sets/:attributeSetId/attributes call.

  • Endpoint

GET <host>/rest/default/V1/products/attribute-sets/9/attributes

  • Response

There are almost 3000 lines in the response. The below table displays a summary of the attributes that are relevant in this post.

Admin label Selected value Attribute ID attribute_code Attribute value
Tax Class Taxable Goods 132 tax_class_id 2
Visibility Catalog, Search 99 visibility 4
Material LumaTech 136 material 148
Pattern Graphic Print 152 pattern 196
Color Gray 93 color 52
Size Not applicable 141 size 168 (small), 169 (medium), 170 (large)

Note: The attribute ID and value number might be different on your installation. Please check the values carefully before using them in their calls.

Get the list of category values

To allow customers to find the product by browsing, you need to assign the product to one or more categories. In our example, we will assign the Dime Tee to the Men, Tops, and Tees categories.

Please use the following call to search for all categories (id is equal to or greater than 0).

GET <host>/rest/default/V1/categories?
searchCriteria[filter_groups][0][filters][0][field]=id&
searchCriteria[filter_groups][0][filters][0][value]=1&
searchCriteria[filter_groups][0][filters][0][condition_type]=gte

Note: The women’s tops and tees have different ids than men’s tops and tees. The value for men’s clothing are:

  • Men - 11
  • Tops - 12
  • Tees - 16

Step 2: Create The Configurable Product

After having the information we need, it is time for us to create the Dime Tee configurable product. Instead of being in the below sample payload, the price or the size will be determined in the simple products. The visibility is set to 4, meaning that the product can be found by browsing or searching. This value will be adjusted for simple products.

Endpoint

POST <host>/rest/default/V1/products

Payload

{
  "product": {
    "sku": "MS-Lime",
    "name": "Lime Tee",
    "attribute_set_id": 9,
    "status": 1,
    "visibility": 4,
    "type_id": "configurable",
    "weight": "0.6",
    "extension_attributes": {
    	"category_links": [
    		{
    			"position": 0,
    			"category_id": "11"
    		},
    		{
    			"position": 1,
    			"category_id": "12"
    		},
    		{
    			"position": 2,
    			"category_id": "16"
    		}
    	]
    },
    "custom_attributes": [
    	{
    		"attribute_code": "description",
    		"value": "The Dime Tee is soft, comfortable and durable. You will love the way you look in this tailored tee shirt."
    	},
    	{
    		"attribute_code": "tax_class_id",
    		"value": "2"
    	},
    	{
    		"attribute_code": "material",
    		"value": "148"
    	},
    	{
    		"attribute_code": "pattern",
    		"value": "196"
    	},
    	{
    		"attribute_code": "color",
    		"value": "52"
    	}
    ]
  }
}

Response

{
    "id": 2078,
    "sku": "MS-Lime",
    "name": "Lime Tee",
    "attribute_set_id": 9,
    "price": 0,
    "status": 1,
    "visibility": 4,
    "type_id": "configurable",
    "created_at": "2017-11-29 19:57:20",
    "updated_at": "2017-11-29 19:57:20",
    "weight": 0.6,
    "extension_attributes": {
        "website_ids": [
            1
        ],
        "category_links": [
            {
                "position": 0,
                "category_id": "11"
            },
            {
                "position": 1,
                "category_id": "12"
            },
            {
                "position": 2,
                "category_id": "16"
            }
        ],
        "stock_item": {
            "item_id": 2078,
            "product_id": 2078,
            "stock_id": 1,
            "qty": 0,
            "is_in_stock": false,
            "is_qty_decimal": false,
            "show_default_notification_message": false,
            "use_config_min_qty": true,
            "min_qty": 0,
            "use_config_min_sale_qty": 1,
            "min_sale_qty": 1,
            "use_config_max_sale_qty": true,
            "max_sale_qty": 10000,
            "use_config_backorders": true,
            "backorders": 0,
            "use_config_notify_stock_qty": true,
            "notify_stock_qty": 1,
            "use_config_qty_increments": true,
            "qty_increments": 0,
            "use_config_enable_qty_inc": true,
            "enable_qty_increments": false,
            "use_config_manage_stock": true,
            "manage_stock": true,
            "low_stock_date": null,
            "is_decimal_divided": false,
            "stock_status_changed_auto": 0
        },
        "configurable_product_options": [],
        "configurable_product_links": []
    },
    "product_links": [],
    "options": [],
    "media_gallery_entries": [],
    "tier_prices": [],
    "custom_attributes": [
        {
            "attribute_code": "description",
            "value": "The Dime Tee is soft, comfortable and durable. You will love the way you look in this tailored tee shirt."
        },
        {
            "attribute_code": "color",
            "value": "52"
        },
        {
            "attribute_code": "category_ids",
            "value": [
                "11",
                "12",
                "16"
            ]
        },
        {
            "attribute_code": "options_container",
            "value": "container2"
        },
        {
            "attribute_code": "required_options",
            "value": "0"
        },
        {
            "attribute_code": "has_options",
            "value": "0"
        },
        {
            "attribute_code": "url_key",
            "value": "champ-tee"
        },
        {
            "attribute_code": "tax_class_id",
            "value": "2"
        },
        {
            "attribute_code": "material",
            "value": "148"
        },
        {
            "attribute_code": "size",
            "value": "91"
        },
        {
            "attribute_code": "pattern",
            "value": "196"
        }
    ]
}

Step 3: Verify The Results

  • Access to your admin panel, navigate to Catalog > Products. Now you will see the products on the grid.
  • Search for Dime on your storefront page. No results are shown.

Conclusion

Above are the details instruction for creating a configurable product with Magento 2 API. With this article, I hope you can manage your online store more effectively. If you have any questions or want to discuss some things related to this post, feel free to leave a comment below!

Image Description
With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.
x