Display categories and their subcategories in Magento using REST API
Asked Answered
C

2

10

Can anyone help me display categories and their subcategories using the 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, i.e. something like this:

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

Thanks in advance.

Chuckhole answered 24/10, 2013 at 5:29 Comment(1)
In my case website/api/rest/products?limit=2 return 404 error, i am using rest, any idea about this error?Shapeless
G
10
/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. I realize "use some other api" is the worst kind of answer but, it is what Magento currently offers.

The alternative is to extend the stock api with your own restful patchwork, but, life is short.

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

Gramicidin answered 24/10, 2013 at 20:35 Comment(0)
A
6

There is indeed still nothing to be found either in Magento 1.x API guide either in the code.

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.

Magento Folder Structure for REST API Model

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) :

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.

Although there seems to be hope in Magento 2.x API list listing those available methods :

DELETE /V1/categories/:categoryId
GET /V1/categories/:categoryId
POST /V1/categories
GET /V1/categories
PUT /V1/categories/:id
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

Acaricide answered 6/4, 2016 at 19:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.