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

时间:2013-10-24 05:29:53

标签: php api magento rest

有人可以帮助我使用REST API显示类别及其子类别吗?

我试过这个网址:

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

但是它显示了 ID 类别而不是我需要的类别,例如:

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

提前致谢。

2 个答案:

答案 0 :(得分:8)

/api/rest/products/:productId/categories

这应该检索分配给特定产品的类别列表。不幸的是,这不会引导您找到您应得的优雅解决方案。

如果您想要一个类别列表,您可能必须使用soap api 来使用catalog_categories.tree method检索类别。我意识到“使用其他api”是最糟糕的答案,但是,这是磁电机目前提供的。

另一种方法是用你自己的宁静拼凑来扩展股票api,但是,生命是短暂的。

如果您真的想做类似的事情那么,这个其他Stack Overflow问题将帮助您入门:  Create new magento Rest api to get category list in magento

答案 1 :(得分:6)

在代码中的Magento 1.x API guide中确实仍然找不到任何东西。

您可以在Mage_Catalog模块的文件夹结构中清楚地看到,代表类别的模型仍然只是产品的子模型,因此它的主要目标仍然只是"仅#34 ;能够知道特定产品与哪些类别相关联。

Magento Folder Structure for REST API Model

这个类别模型的类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;
}

我不确定Magento是否仍会在框架的1.x版本上为REST API付出任何努力。

虽然Magento 2.x API list列出了那些可用的方法似乎有希望:

  

DELETE / V1 / categories /:categoryId
  GET / V1 /类别/:categoryId
  POST / V1 /类别
  GET / V1 /类别
  PUT / V1 / categories /:id
  PUT / V1 / categories /:categoryId / move

此外,该代码还提供了获取类别树的可能性 请参阅herethere

所以在Magento 2.x

下肯定是可能的