1

I am using Magento 2 rest api for listing all the catgories.

{{production_url}}/index.php/rest/V1/categories

It will return all the categories,

{
    "id": 2,
    "parent_id": 1,
    "name": "Default Category",
    "is_active": true,
    "position": 1,
    "level": 1,
    "product_count": 0,
    "children_data": [{
        "id": 3,
        "parent_id": 2,
        "name": "T-shirts",
        "is_active": true,
        "position": 1,
        "level": 2,
        "product_count": 8,
        "children_data": []
    }, {
        "id": 4,
        "parent_id": 2,
        "name": "Phants",
        "is_active": true,
        "position": 2,
        "level": 2,
        "product_count": 0,
        "children_data": []
    }, {
        "id": 5,
        "parent_id": 2,
        "name": "Chridar",
        "is_active": true,
        "position": 3,
        "level": 2,
        "product_count": 0,
        "children_data": []
    }]
}

But i need custom attributes for every categories in the result.But now i have to call the below api for getting custom attributes.

{{production_url}}/index.php/rest/V1/categories/3

It will return ,

{
    "id": 3,
    "parent_id": 2,
    "name": "T-shirts",
    "is_active": true,
    "position": 1,
    "level": 2,
    "children": "",
    "created_at": "2017-06-02 11:21:16",
    "updated_at": "2017-06-02 11:21:16",
    "path": "1/2/3",
    "available_sort_by": [],
    "include_in_menu": true,
    "custom_attributes": [
        {
            "attribute_code": "description",
            "value": "<p>retest</p>"
        },
        {
            "attribute_code": "image",
            "value": "Screen_Shot_2017-06-16_at_4.06.35_PM.png"
        },
        {
            "attribute_code": "display_mode",
            "value": "PRODUCTS"
        },
        {
            "attribute_code": "is_anchor",
            "value": "1"
        },
        {
            "attribute_code": "path",
            "value": "1/2/3"
        },
        {
            "attribute_code": "children_count",
            "value": "0"
        },
        {
            "attribute_code": "custom_use_parent_settings",
            "value": "0"
        },
        {
            "attribute_code": "custom_apply_to_products",
            "value": "0"
        },
        {
            "attribute_code": "url_key",
            "value": "redwine"
        },
        {
            "attribute_code": "url_path",
            "value": "redwine"
        }
    ]
}

Suppose if there are n catgories i need to call n api for getting custom attributes.Is there any single api for getting all the attributes for all categories in a single API?

2
  • Did you get a solution ??
    – Jsparo30
    Jul 24, 2017 at 10:32
  • @Jsparo30 I didn't get the solution
    – Muhsin
    Jul 24, 2017 at 15:02

2 Answers 2

0

Method "\Magento\Catalog\Model\Category\Tree::getTree()" where object Tree fills with category data, it uses methods of category main model "Magento\Catalog\Model\Category", which implements interface "Magento\Catalog\Api\Data\CategoryTreeInterface".

For example, "$tree->setProductCount($node->getProductCount())".

And namely Category model implements CategoryTreeInterface, not it's parent AbstractModel, so you can't use setData() function to add your attribute to tree data.

Need to implement few things to reach this goal:

  • preference to "Magento\Catalog\Model\Category" model to add methods like "getMyAttributeValue()" and "setMyAttributeValue()"
  • plugin "after" to "\Magento\Catalog\Model\Category\Tree::getTree()" to add your attribute value to Tree. For example, "$tree->setMyAttributeValue($node->getMyAttributeValue())"

That's it.

-1

ok i have the same issue and created this php script that takes category id and return all attributes in json format

https://github.com/salehawal/magento_category_attributes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.