Bharat Chodvadiya Bharat Chodvadiya - 2 years ago 253
PHP Question

Create new magento Rest api to get category list in magento

Does anyone know how to create new magento rest api to get category listing.

I already saw this link
magento rest api but this not fetch category listings.

If any one knows then please solve this..

Answer Source

First create basic module using this and then you have to create following file and add this file in given path.

Module path = app/code/local/Restapi/Categories

1) app/code/local/Restapi/Categories/etc/api2.xml

<?xml version="1.0"?>
        <catalog translate="title" module="api2">
        <categories translate="title" module="api2">
              <!--  <create>1</create>
                    <delete>1</delete>  --> 
              <!--  <create>1</create>
                    <delete>1</delete>  --> 
              <!--  <create>1</create>
                    <delete>1</delete>  --> 
                <category_id>Category ID</category_id>
                <parent_id>Category Parent ID</parent_id>
                <child_id>Category Child List</child_id>

Here cat_id is category id which we pass to fetch child category.

2) app/code/local/Restapi/Categories/Model/Api2/Category/Rest/Customer/V1.php

class Restapi_Categories_Model_Api2_Category_Rest_Customer_V1 extends Restapi_Categories_Model_Api2_Category
 * Retrieve list of category list.
 * @return array
protected function _retrieveCollection()
    $ruleId = $this->getRequest()->getParam('cat_id');

//  $cat_mod = Mage::getModel('catalog/category')->load($ruleId)->toArray();
    $cats = Mage::getModel('catalog/category')->load($ruleId);
    $subcats  = Mage::getModel('catalog/category')->load($ruleId)->getChildren();

    $cur_category = array();

    $node['category_id'] = $ruleId;
    $node['name'] = $cats->getName();
    $node['parent_id'] = $cats->getParentId();
    $node['child_id'] = $subcats;
        $node['active'] = 1;
        $node['active'] = 0;
    $node['level'] = $cats->getLevel();
    $node['position'] = $cats->getPosition();

    $cur_category[] = $node;

//  $subcats  = Mage::getModel('catalog/category')->load($ruleId)->getAllChildren();
//  $subcats  = Mage::getModel('catalog/category')->load($ruleId)->getChildren();

    if($subcats != '') 
        foreach(explode(',',$subcats) as $subCatid)
            $_category = Mage::getModel('catalog/category')->load($subCatid);
            $childcats  = Mage::getModel('catalog/category')->load($subCatid)->getChildren();

            $node['category_id'] = $subCatid;
            $node['name'] = $_category->getName();
            $node['parent_id'] = $_category->getParentId();
            $node['child_id'] = $childcats;
                $node['active'] = 1;
                $node['active'] = 0;
            $node['level'] = $_category->getLevel();
            $node['position'] = $_category->getPosition();

            $cur_category[] = $node;


    return $cur_category;       

you also add this same V1.php file in this path if you have other user like admin and guest.



Just you want to change the class path in V1.php file like Customer = Admin.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download