Magento 2 API Create Simple Products
Being the most basic product type in Magento, a simple product is a physical item with a single SKU.
Thanks to a variety of pricing and input controls, simple products are possible to sell variations of the product.
In this article, I will instruct you to create a simple product with Magento 2 API through the below steps:
- Step 1: Plan The Product
- Step 2: Create The First Simple Product
- Step 3: Create The Other Simple Products
- Step 4: Verify The Results
Step 1: Plan The Product
Define The Simple Product Characteristics
Note:
- The payloads for creating a simple product and a configurable product are similar, with the following exceptions:
- The simple product
sku
attaches the configurable option to the configurable productsku
. - The
name
shows the size. - The type_id is set to simple.
- The visibility is set to 1, meaning that the simple product should not be displayed on the store.
- The price and size attributes are specified.
- The simple product
- Although it is not required, the simple product payload also contains
stock_item
information. By default, out-of-stock items are hidden, so adding stock will make the product visible on the website.
Assuming that we need to create a small Dime t-shirt as a single product in Magento 2. The following table displays the attributes of the men t-shirt we are creating:
Characteristic | Description |
---|---|
Attribute Set | Top |
Product Name | Dime Tee Small |
SKU | MS-Dime-S |
Price | 30 |
Tax Class | Taxable Goods |
Weight | 0.6 |
Categories | Men, Tops, Tees |
Visibility | Not display on the store |
Material | LumaTech |
Pattern | Graphic Print |
Color | Gray |
Size | Small |
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 product, we have to make several calls.
Step 2: Create The First Simple Product
Please use the below code sample to create the first simple product.
Endpoint
POST <host>/rest/default/V1/products
Payload
{
"product": {
"sku": "MS-Dime-S",
"name": "Dime Tee Small",
"attribute_set_id": 9,
"price": 25,
"status": 1,
"visibility": 1,
"type_id": "simple",
"weight": "0.6",
"extension_attributes": {
"category_links": [
{
"position": 0,
"category_id": "11"
},
{
"position": 1,
"category_id": "12"
},
{
"position": 2,
"category_id": "16"
}
],
"stock_item": {
"qty": "10",
"is_in_stock": true
}
},
"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"
},
{
"attribute_code": "size",
"value": "168"
}
]
}
}
Response
{
"id": 2079,
"sku": "MS-Dime-S",
"name": "Dime Tee Small",
"attribute_set_id": 9,
"price": 25,
"status": 1,
"visibility": 1,
"type_id": "simple",
"created_at": "2017-11-29 20:40:07",
"updated_at": "2017-11-29 20:40:07",
"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": 2079,
"product_id": 2079,
"stock_id": 1,
"qty": 10,
"is_in_stock": true,
"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
}
},
"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-small"
},
{
"attribute_code": "tax_class_id",
"value": "2"
},
{
"attribute_code": "material",
"value": "148"
},
{
"attribute_code": "size",
"value": "168"
},
{
"attribute_code": "pattern",
"value": "196"
}
]
}
Step 3: Create The Other Simple Products
If you want to create other simple products (for example Dime Tee in medium or large size), repeat the call with the following changes to the payload:
Attribute | Medium Value | Large Value |
---|---|---|
sku | MS-Dime-M | MS-Dime-L |
name | Dime Tee Medium | Dime Tee Large |
size | 169 | 170 |
Step 4: 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.
Conclusions
Above are the detailed instruction for creating a simple 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!
Enjoyed the tutorial? Spread it to your friends!