Product attribute create/update using rest api and assign it to attribute set
-
16-04-2021 - |
Question
I am creating product attributes using rest API with the below request body.
End point rest/V1/products/attributes/
{
"attribute": {
"is_wysiwyg_enabled": false,
"is_html_allowed_on_front": false,
"used_for_sort_by": false,
"is_filterable": true,
"is_filterable_in_search": true,
"is_used_in_grid": true,
"is_visible_in_grid": false,
"is_filterable_in_grid": true,
"position": 0,
"apply_to": [],
"is_searchable": "1",
"is_visible_in_advanced_search": "1",
"is_comparable": "1",
"is_used_for_promo_rules": "0",
"is_visible_on_front": "0",
"used_in_product_listing": "1",
"is_visible": true,
"scope": "global",
"attribute_code": "text_api_attribute",
"frontend_input": "text",
"entity_type_id": "4",
"is_required": false,
"is_user_defined": true,
"default_frontend_label": "Att Label updated",
"frontend_labels": null,
"backend_type": "int",
"default_value": "",
"is_unique": "0"
}
}
it's get created successfully but how can I assign this attribute to the attribute set.tried with group: General
but its not defined in interface so it throw error.
also there is an error when try to update using PUT
method rest/V1/products/attributes/rest/V1/products/attributes/
{
"message": "%1 already exists.",
"parameters": [
"Attribute with the same code"
]
}
Solution
Try using the endpoint POST /rest/default/V1/products/attribute-sets/attributes
with a payload like this:
{
"attributeSetId": 4,
"attributeGroupId": 7,
"attributeCode": "text_api_attribute",
"sortOrder": 99
}
Note that my attribute set ID is 4 which is the 'Default' attribute set and the attribute group is 7 which is the Default attribute set's 'Product details' group. IDs taken from the db from tables eav_attribute_set
and eav_attribute_group
.
Reference from Redoc: https://magento.redoc.ly/2.3.6-admin/tag/productsattribute-setsattributes#operation/catalogProductAttributeManagementV1AssignPost