简体   繁体   English

使用 REST API 在 Magento 中显示类别及其子类别

[英]Display categories and their subcategories in Magento using REST API

Can anyone help me display categories and their subcategories using the REST API?任何人都可以帮助我使用 REST API 显示类别及其子类别吗?

I have tried this url:我试过这个网址:

http://localhost/magento/api/rest/products/:productId/categories

But it displays category ID instead of what I'd need, ie something like this:但它显示类别ID而不是我需要的,即这样的东西:

http://localhost/magento/api/rest/categories

Thanks in advance.提前致谢。

/api/rest/products/:productId/categories

This should retrieve a list of categories assigned to a specific product.这应该检索分配给特定产品的类别列表。 Unfortunately, this won't lead you to the elegant solution you deserve.不幸的是,这不会引导您找到您应得的优雅解决方案。

If you want a list of categories you may have to use the soap api to retrieve the categories using the catalog_categories.tree method .如果您想要一个类别列表,您可能必须使用soap api使用catalog_categories.tree 方法检索类别。 I realize "use some other api" is the worst kind of answer but, it is what Magento currently offers.我意识到“使用其他 api”是最糟糕的答案,但是,这是 Magento 目前提供的。

The alternative is to extend the stock api with your own restful patchwork, but, life is short.另一种选择是使用您自己的宁静拼凑来扩展库存 api,但是,生命是短暂的。

If you really want to do something like that then, this other Stack Overflow question will help get you started: Create new magento Rest api to get category list in magento如果你真的想做这样的事情,那么另一个堆栈溢出问题将帮助你开始: 创建新的 magento Rest api to get category list in magento

There is indeed still nothing to be found either in Magento 1.x API guide either in the code.Magento 1.x API 指南或代码中确实仍然没有找到任何内容。

You can clearly see in the folder structure of the Mage_Catalog module that the model representing categories are still only sub models of the product, so it's main goal is still "only" to be able to know in which categories a specific product is linked to.您可以在Mage_Catalog模块的文件夹结构中清楚地看到,表示类别的模型仍然只是产品的子模型,因此其主要目标仍然是“仅”能够知道特定产品链接到哪些类别。

REST API 模型的 Magento 文件夹结构

And the code of this category model being of class Mage_Catalog_Model_Api2_Product_Category_Rest doesn't look like anything as changed from the previous answer, sadly (around line 61 in the latest version 1.9.2.4) : Mage_Catalog_Model_Api2_Product_Category_Rest是,这个类别模型的代码属于Mage_Catalog_Model_Api2_Product_Category_Rest类,看起来与之前的答案没有任何变化(最新版本 1.9.2.4 中的第 61 行左右):

protected function _retrieveCollection()
{
    $return = array();

    foreach ($this->_getCategoryIds() as $categoryId) {
        $return[] = array('category_id' => $categoryId);
    }
    return $return;
}

And I am unsure if Magento is still going to put any effort in REST API on the version 1.x of the framework.而且我不确定 Magento 是否仍会在框架的 1.x 版上为 REST API 付出任何努力。

Although there seems to be hope in Magento 2.x API list listing those available methods :尽管Magento 2.x API 列表中似乎有希望列出这些可用方法:

DELETE /V1/categories/:categoryId删除 /V1/categories/:categoryId
GET /V1/categories/:categoryId获取 /V1/categories/:categoryId
POST /V1/categories POST /V1/categories
GET /V1/categories获取/V1/类别
PUT /V1/categories/:id PUT /V1/categories/:id
PUT /V1/categories/:categoryId/move PUT /V1/categories/:categoryId/move

Also the code offers possibilities to get the categories tree.该代码还提供了获取类别树的可能性。
See here and there .这里那里

So it is definitely something possible under Magento 2.x所以在 Magento 2.x 下绝对是可能的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM