-
Notifications
You must be signed in to change notification settings - Fork 248
Create a configurable product [Web API Tutorial]
Lets keep the Default attribute set from changing and create one new attribute set where we can add anything we will need.
Endpoint
POST http://<host>/rest/all/V1/eav/attribute-sets
HTTP headers
Content-Type: application/json
Authorization: Bearer <admin_token>
Payload
{
"entityTypeCode": "catalog_product",
"attributeSet": {
"attribute_set_name": "Configurable Hoodies and Sweatshirts",
"entity_type_id": 4
},
"skeletonId": 4
}
Response
Full information about the attribute set that was created with assigned attribute_set_id
Complete cURL request sample
curl -X POST "$endpoint/all/V1/eav/attribute-sets" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $admin_token" \
-d '{"entityTypeCode":"catalog_product","attributeSet":{"attribute_set_name":"Configurable Hoodies and Sweatshirts","entity_type_id":4},"skeletonId":4}' | json_pp
Endpoint
POST http://<host>/rest/all/V1/products/attributes
HTTP headers
Content-Type: application/json
Authorization: Bearer <admin_token>
Payload
{
"attribute": {
"attribute_code": "size",
"entity_type_id": "4",
"default_frontend_label": "Size",
"frontend_labels": [{
"store_id": 1,
"label": "Size"
},{
"store_id": 2,
"label": "Size"
}],
"is_required": true,
"default_value": "",
"frontend_input": "select",
"is_visible_on_front": true,
"is_searchable": true,
"is_visible_in_advanced_search": true,
"is_filterable": true,
"is_filterable_in_search": true,
"options": [{
"label": "S",
"value": "S",
"sort_order": 100,
"is_default": true,
"store_labels": [{
"store_id": 2,
"label": "Size S"
}, {
"store_id": 1,
"label": "Size S"
}]
}, {
"label": "M",
"value": "M",
"sort_order": 0,
"is_default": false,
"store_labels": [{
"store_id": 2,
"label": "Size M"
}, {
"store_id": 1,
"label": "Size M"
}]
}, {
"label": "L",
"value": "L",
"sort_order": 0,
"is_default": false,
"store_labels": [{
"store_id": 2,
"label": "Size L"
}, {
"store_id": 1,
"label": "Size L"
}]
}, {
"label": "XL",
"value": "XL",
"sort_order": 0,
"is_default": false,
"store_labels": [{
"store_id": 2,
"label": "Size XL"
}, {
"store_id": 1,
"label": "Size XL"
}]
}, {
"label": "XXL",
"value": "XXL",
"sort_order": 0,
"is_default": false,
"store_labels": [{
"store_id": 2,
"label": "Size XXL"
}, {
"store_id": 1,
"label": "Size XXL"
}]
}]
}
}
Complete cUrl request
curl -X POST "$endpoint/all/V1/products/attributes" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $admin_token" \
-d '{"attribute":{"attribute_code":"size","entity_type_id":"4","default_frontend_label":"Size","frontend_labels":[{"store_id":1,"label":"Size"},{"store_id":2,"label":"Size"}],"is_required":true,"default_value":"","frontend_input":"select","is_visible_on_front":true,"is_searchable":true,"is_visible_in_advanced_search":true,"is_filterable":true,"is_filterable_in_search":true,"options":[{"label":"S","value":"S","sort_order":100,"is_default":true,"store_labels":[{"store_id":2,"label":"Size S"},{"store_id":1,"label":"Size S"}]},{"label":"M","value":"M","sort_order":0,"is_default":false,"store_labels":[{"store_id":2,"label":"Size M"},{"store_id":1,"label":"Size M"}]},{"label":"L","value":"L","sort_order":0,"is_default":false,"store_labels":[{"store_id":2,"label":"Size L"},{"store_id":1,"label":"Size L"}]},{"label":"XL","value":"XL","sort_order":0,"is_default":false,"store_labels":[{"store_id":2,"label":"Size XL"},{"store_id":1,"label":"Size XL"}]},{"label":"XXL","value":"XXL","sort_order":0,"is_default":false,"store_labels":[{"store_id":2,"label":"Size XXL"},{"store_id":1,"label":"Size XXL"}]}]}}' | json_pp
The attributeSetId
, attributeGroupId
, attributeCode
, sortOrder
attributes are required. The value for attributeSetId
you have already obtained in Create an attribute set for configurations step. What about other attributes? Read about Data preparation before assigning attribute to an attribute set
Endpoint
POST /V1/products/attribute-sets/attributes
HTTP headers
Content-Type: application/json
Authorization: Bearer <admin_token>
Payload
{
"attributeSetId": 9,
"attributeGroupId": 30,
"attributeCode": "size",
"sortOrder": 30
}
Response
Complete cURL request
curl -X POST "$endpoint/all/V1/products/attribute-sets/attributes" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $admin_token" \
-d '{"attributeSetId":9,"attributeGroupId":30,"attributeCode":"size","sortOrder":30}' | json_pp -json_opt allow_nonref
During this operation, you will get id
of the category. You need to remember this value.
Anyway, you always can get the value of the id
property from catalogCategoryListV1
interface by addressing it from your local API reference.
In case you are confused with searchCriteria
you may try to run this snippet:
curl -X GET "$endpoint/all/V1/categories/list?searchCriteria%5BpageSize%5D=100&searchCriteria%5BcurrentPage%5D=1" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $admin_token" | grep -i '"id"'
Endpoint
POST http://<host>/rest/all/V1/categories"
HTTP headers
Content-Type: application/json
Authorization: Bearer <admin_token>
Payload
{
"category": {
"parent_id": 2,
"name": "Hoodies & Sweatshirts",
"is_active": true,
"level": 2,
"include_in_menu": true,
"custom_attributes": [{
"attribute_code": "is_anchor",
"value": "1"
}, {
"attribute_code": "url_key",
"value": "hoodies-sweatshirts"
}, {
"attribute_code": "url_path",
"value": "hoodies-sweatshirts"
}]
}
}
Response
Full information about the category that was created and id
that was assigned to the category
Complete cURL request sample
curl -X POST "$endpoint/all/V1/categories" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $admin_token" \
-d '{"category":{"parent_id":2,"name":"Hoodies & Sweatshirts","is_active":true,"level":2,"include_in_menu":true,"custom_attributes":[{"attribute_code":"is_anchor","value":"1"},{"attribute_code":"url_key","value":"hoodies-sweatshirts"},{"attribute_code":"url_path","value":"hoodies-sweatshirts"}]}}' | json_pp | grep -i '"id"'
Endpoint
POST http://<host>/rest/test/V1/products
HTTP headers
Content-Type: application/json
Authorization: Bearer <admin_token>
Payload
{
"product": {
"sku": "Swim Warm-Up Hoodie",
"name": "Swim Warm-Up Hoodie",
"status": 1,
"type_id": "configurable",
"attribute_set_id": 9,
"extension_attributes": {
"category_links": [{
"position": 0,
"category_id": "5"
}],
"stock_item": {
"is_in_stock": true
}
}
}
}
Response Full information about the product that was created
Complete cURL request sample
configurable_product_SKU="Swim Warm-Up Hoodie"; category_id="5" && curl -X POST "$endpoint/test/V1/products" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $admin_token" \
-d '{"product":{"sku":"'"$configurable_product_SKU"'","name":"'"$configurable_product_SKU"'","status":1,"type_id":"configurable","attribute_set_id":9,"extension_attributes":{"category_links":[{"position":0,"category_id":"'"$category_id"'"}],"stock_item":{"is_in_stock":true}}}}'
Send each payload as a seperate request on the same endpoint with the same headers.
Endpoint
POST http://<host>/rest/test/V1/products
HTTP headers
Content-Type: application/json
Authorization: Bearer <admin_token>
Payload
{
"product": {
"sku": "Swim Warm-Up Hoodie - S",
"name": "Swim Warm-Up Hoodie - S",
"attribute_set_id": 9,
"price": 90,
"status": 1,
"visibility": 1,
"type_id": "simple",
"weight": "0.6",
"extension_attributes": {
"stock_item": {
"qty": "70",
"is_in_stock": true
}
},
"custom_attributes": [{
"attribute_code": "size",
"value": "4"
}]
}
}
Send this payload 4 more times, but modify all of these values each time:
sku & name | qty | attribute_code | value |
---|---|---|---|
Swim Warm-Up Hoodie - M | 80 | size | 5 |
Swim Warm-Up Hoodie - L | 100 | size | 6 |
Swim Warm-Up Hoodie - XL | 75 | size | 7 |
Swim Warm-Up Hoodie - XXL | 40 | size | 8 |
Complete cURL request
configurable_product_SKU_V1="Swim Warm-Up Hoodie - S" && curl -X POST "$endpoint/test/V1/products" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $admin_token" \
-d '{"product":{"sku":"'"$configurable_product_SKU_V1"'","name":"'"$configurable_product_SKU_V1"'","attribute_set_id":9,"price":90,"status":1,"visibility":1,"type_id":"simple","weight":"0.6","extension_attributes":{"stock_item":{"qty":"70","is_in_stock":true}},"custom_attributes":[{"attribute_code":"size","value":"4"}]}}' | json_pp
configurable_product_SKU_V2="Swim Warm-Up Hoodie - M" && curl -X POST "$endpoint/test/V1/products" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $admin_token" \
-d '{"product":{"sku":"'"$configurable_product_SKU_V2"'","name":"'"$configurable_product_SKU_V2"'","attribute_set_id":9,"price":90,"status":1,"visibility":1,"type_id":"simple","weight":"0.72","extension_attributes":{"stock_item":{"qty":"80","is_in_stock":true}},"custom_attributes":[{"attribute_code":"size","value":"5"}]}}' | json_pp
configurable_product_SKU_V3="Swim Warm-Up Hoodie - L" && curl -X POST "$endpoint/test/V1/products" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $admin_token" \
-d '{"product":{"sku":"'"$configurable_product_SKU_V3"'","name":"'"$configurable_product_SKU_V3"'","attribute_set_id":9,"price":90,"status":1,"visibility":1,"type_id":"simple","weight":"0.8","extension_attributes":{"stock_item":{"qty":"100","is_in_stock":true}},"custom_attributes":[{"attribute_code":"size","value":"6"}]}}' | json_pp
configurable_product_SKU_V4="Swim Warm-Up Hoodie - XL" && curl -X POST "$endpoint/test/V1/products" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $admin_token" \
-d '{"product":{"sku":"'"$configurable_product_SKU_V4"'","name":"'"$configurable_product_SKU_V4"'","attribute_set_id":9,"price":90,"status":1,"visibility":1,"type_id":"simple","weight":"1","extension_attributes":{"stock_item":{"qty":"75","is_in_stock":true}},"custom_attributes":[{"attribute_code":"size","value":"7"}]}}' | json_pp
configurable_product_SKU_V5="Swim Warm-Up Hoodie - XXL" && curl -X POST "$endpoint/test/V1/products" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $admin_token" \
-d '{"product":{"sku":"'"$configurable_product_SKU_V5"'","name":"'"$configurable_product_SKU_V5"'","attribute_set_id":9,"price":90,"status":1,"visibility":1,"type_id":"simple","weight":"1.12","extension_attributes":{"stock_item":{"qty":"40","is_in_stock":true}},"custom_attributes":[{"attribute_code":"size","value":"8"}]}}' | json_pp
Endpoint
Payload
{
"option": {
"attribute_id": "135",
"label": "Size",
"position": 0,
"is_use_default": false,
"values": [{
"value_index": 4
}, {
"value_index": 5
}, {
"value_index": 6
}, {
"value_index": 7
}, {
"value_index": 8
}]
}
}
Complete cURL request
productSKUURLencoded=$(echo "$configurable_product_SKU" | sed 's/ /%20/g') && curl -X POST "$endpoint/test/V1/configurable-products/$productSKUURLencoded/options" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $admin_token" \
-d '{"option":{"attribute_id":"135","label":"Size","position":0,"is_use_default":false,"values":[{"value_index":4},{"value_index":5},{"value_index":6},{"value_index":7},{"value_index":8}]}}'
📖 Table of Contents | Next Step |
Multi-Source Inventory developed by Magento 2 Community
- Technical Vision. Catalog Inventory
- Installation Guide
- List of Inventory APIs and their legacy analogs
- MSI Roadmap
- Known Issues in Order Lifecycle
- MSI User Guide
- 2.3 LIVE User Guide
- MSI Release Notes and Installation
- Overview
- Get Started with MSI
- MSI features and processes
- Global and Product Settings
- Configure Source Selection Algorithm
- Create Sources
- Create Stock
- Assign Inventory and Product Notifications
- Configure MSI backorders
- MSI Import and Export Product Data
- Mass Action Tool
- Shipment and Order Management
- CLI reference
- Reports and MSI
- MSI FAQs
- DevDocs Documentation
- Manage Inventory Management Modules (install/upgrade info)
- Inventory Management
- Reservations
- Inventory CLI reference
- Inventory API reference
- Inventory In-Store Pickup API reference
- Order Processing with Inventory Management
- Managing sources
- Managing stocks
- Link and unlink stocks and sources
- Manage source items
- Perform bulk actions
- Manage Low-Quantity Notifications
- Check salable quantities
- Manage source selection algorithms
- User Stories
- Support of Store Pickup for MSI
- Product list assignment per Source
- Source assignment per Product
- Stocks to Sales Channel Mapping
- Adapt Product Import/Export to support multi Sourcing
- Introduce SourceCode attribute for Source and SourceItem entities
- Assign Source Selector for Processing of Returns Credit Memo
- User Scenarios:
- Technical Designs:
- Module Structure in MSI
- When should an interface go into the Model directory and when should it go in the Api directory?
- Source and Stock Item configuration Design and DB structure
- Stock and Source Configuration design
- Open Technical Questions
- Inconsistent saving of Stock Data
- Source API
- Source WebAPI
- Sources to Sales Channels mapping
- Service Contracts MSI
- Salable Quantity Calculation and Mechanism of Reservations
- StockItem indexation
- Web API and How To cover them with Functional Testing
- Source Selection Algorithms
- Validation of Domain Entities
- PHP 7 Syntax usage for Magento contribution
- The first step towards pre generated IDs. And how this will improve your Integration tests
- The Concept of Default Source and Domain Driven Design
- Extension Point of Product Import/Export
- Source Selection Algorithm
- SourceItem Entity Extension
- Design Document for changing SerializerInterface
- Stock Management for Order Cancelation
- Admin UI
- MFTF Extension Tests
- Weekly MSI Demos
- Tutorials