Magento 2 API Add Simple Products To Configurable Product
A configurable product is a parent of multiple simple products. After creating the configurable product that defines the basic characteristics of the product and the simple product for each configurable attribute, you need to add each simple product to the configurable product. When all simple products are added to a configurable product, your customers can easily choose the product options they want, and then add them to their carts. Hence, you can enhance your customer experiences and grow your store.
Therefore, it’s essential to add simple products to your configurable products. In this post, you will know how to do it right away.
Let’s explore the process of adding simple products to the configurable product with Magento 2 API through the below steps!
- Step 1: Set The Configurable Attribute
- Step 2: Link The Simple Products To The Configurable Product
- Step 3: Verify The Results
Step 1: Set The Configurable Attribute
In this post, I take an example of creating a gray t-shirt that comes in three sizes, including small, medium, and large.
Assuming that we have created the Dime Tee configurable products and multiple simple products for each size, now we need to assign size
as the configurable attribute and add the simple products to the configurable product.
- To assign the specified
attribute_id
to be the configurable attribute, you have to use thePOST V1/configurable-products/:sku/options
. - Please specify the
SKU
of the configurable product in the URI. - The value defined for the
value_index
must be unique within the system.
Endpoint
POST <host>/rest/default/V1/configurable-products/MS-Dime/options
Payload
{
"option": {
"attribute_id": "141",
"label": "Size",
"position": 0,
"is_use_default": true,
"values": [
{
"value_index": 9
}
]
}
}
Response
A configurable option ID number, for instance “335”.
Step 2: Link The Simple Products To The Configurable Product
The call to link a simple (child) product to the configurable product accepts only one childSku
value.
Hence, you must repeat this call for the MS-Dime-M
and MS-Dime-L
products.
Endpoint
POST <host>/rest/default/V1/configurable-products/MS-Dime/child
Payload
{
"childSku": "MS-Dime-S"
}
Response
true
Step 3: Verify The Results
- Access to your admin panel, navigate to
Catalog > Products
. Now you will see the products on the grid. Click on the Dime Tee configurable product and expand the Configurations section. - Search for Dime on your storefront page, and you will get the results of Dime Tee with three sizes options.
- Make the call
GET /V1/products/MS-Dime
. There are the configurable_product_options and configurable_product_links arrays in the response.
"configurable_product_options": [
{
"id": 338,
"attribute_id": "141",
"label": "Size",
"position": 0,
"values": [
{
"value_index": 168
},
{
"value_index": 169
},
{
"value_index": 170
}
],
"product_id": 2078
}
],
"configurable_product_links": [
2079,
2080,
2081
]
},
Conclusions
Above are the detailed instructions for adding simple products to the configurable products with Magento 2 API. With this post, I hope you can offer better choices to your customers and develop your business effectively. If you have any questions or want to discuss something related to this post, feel free to leave a comment below.
Enjoyed the tutorial? Spread it to your friends!