在 Magento 2 REST Api 中获取所有具有自定义属性的类别

问题描述 投票:0回答:3

我正在使用 Magento 2 rest api 列出所有类别。

{{production_url}}/index.php/rest/V1/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": []
    }]
}

但是我需要结果中每个类别的自定义属性。但是现在我必须调用下面的 api 来获取自定义属性。

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

它会回来的,

{
    "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"
        }
    ]
}

假设如果有

n
类别我需要调用
n
api来获取自定义属性。是否有任何单一的 API 可以在单个 API 中获取所有类别的所有属性?

php rest magento magento2
3个回答
3
投票

Magento Api CatalogTreeInterface 不扩展 Magento\Framework\Api\ExtensibleDataInterface 这意味着自定义属性或扩展属性不能添加到树响应。对我来说唯一的解决方法是创建我自己的模块和一个新的 api 调用来扩展树接口以添加我的自定义属性。


2
投票

我想鲁本是对的。我已经构建了 Magento 扩展以将图像属性添加到类别树中,请看一下https://github.com/troublediehard/mma-customapi


0
投票

Method "\Magento\Catalog\Model\Category\Tree::getTree()" 其中对象 Tree 填充类别数据,它使用类别主模型 "Magento\Catalog\Model\Category" 的方法,实现了接口 "Magento\目录 \Api\Data\CategoryTreeInterface".

例如,“$tree->setProductCount($node->getProductCount())”。

也就是说,Category 模型实现了 CategoryTreeInterface,而不是它的父 AbstractModel,因此您不能使用 setData() 函数将属性添加到树数据中。

需要执行一些事情来实现这个目标:

  • 偏好“Magento\Catalog\Model\Category”模型以添加“getMyAttributeValue()”和“setMyAttributeValue()”等方法
  • 插件“after”到“\Magento\Catalog\Model\Category\Tree::getTree()”将你的属性值添加到树。例如,“$tree->setMyAttributeValue($node->getMyAttributeValue())”

就是这样。

© www.soinside.com 2019 - 2024. All rights reserved.