Create new magento Rest api to get category list in magento
First create basic module using http://www.silksoftware.com/magento-module-creator/ 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"?>
<config>
<api2>
<resource_groups>
<catalog translate="title" module="api2">
<title>Catalog</title>
<sort_order>10</sort_order>
</catalog>
</resource_groups>
<resources>
<categories translate="title" module="api2">
<group>catalog</group>
<model>categories/api2_category</model>
<title>Categories</title>
<sort_order>10</sort_order>
<privileges>
<admin>
<retrieve>1</retrieve>
<!-- <create>1</create>
<update>1</update>
<delete>1</delete> -->
</admin>
<customer>
<retrieve>1</retrieve>
<!-- <create>1</create>
<update>1</update>
<delete>1</delete> -->
</customer>
<guest>
<retrieve>1</retrieve>
<!-- <create>1</create>
<update>1</update>
<delete>1</delete> -->
</guest>
</privileges>
<attributes>
<category_id>Category ID</category_id>
<name>Name</name>
<parent_id>Category Parent ID</parent_id>
<child_id>Category Child List</child_id>
<active>Active</active>
<level>Level</level>
<position>Position</position>
</attributes>
<routes>
<route>
<route>/categories/:cat_id</route>
<action_type>collection</action_type>
</route>
</routes>
<versions>1</versions>
</categories>
</resources>
</api2>
</config>
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
<?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;
if($cats->getIsActive()){
$node['active'] = 1;
}else{
$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;
if($_category->getIsActive()){
$node['active'] = 1;
}else{
$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.
app/code/local/Restapi/Categories/Model/Api2/Category/Rest/Admin/V1.php
app/code/local/Restapi/Categories/Model/Api2/Category/Rest/Guest/V1.php
Just you want to change the class path in V1.php file like Customer = Admin.
Related videos on Youtube
Bharat Chodvadiya
I am Bharat Chodvadiya, working as a PHP Web Developer in india. Area of interest are PHP, MySQl, Magento, Wordpress, jQuery, Ajax, Javascript, Kendo UI, JSON, XML, HTML, HTML5, CSS, Web Design, Social API, Facebook api, Twitter api, Linkedin api.
Updated on June 25, 2022Comments
-
Bharat Chodvadiya almost 2 years
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..
-
fikr4n over 9 yearsHey, I got exactly the same problem and tried your solution in magento CE 1.9, but it is not working. Instead I got the following error: error 404, message:"Request does not match any route." any help?
-
fikr4n over 9 yearsPrevious error is resolved, but still not worked. Error message now: "Resource not found".
-
Bharat Chodvadiya over 9 yearssend me your code to [email protected]. i will check it.
-
fikr4n over 9 yearsThanks, your example was simply not complete :). I had not created class
Restapi_Categories_Model_Api2_Category
. -
Rouzbeh over 9 yearsWhat's inside the class
Restapi_Categories_Model_Api2_Category
? And what's the REST URL? -
Pushpendra almost 9 years@BornToCode and Bharat can you check this question stackoverflow.com/questions/30102225/custom-rest-api-in-magento
-
Pushpendra almost 9 years@BharatChodvadiya Hey can you tell how I can get subcategroies list and then how to access them and then subsub category of subcategory till end.
-
Manoj almost 8 yearshow to call this? i means does it [BASE_URL]/api/rest/categories ??
-
Bharat Chodvadiya over 6 yearsYou can call using this url. magentohost/api/rest/categories/:cat_id. Here magentohost is your domain and :cat_id is the parent category id which needs to list parent and child category list. If you need to list parent and sub category of child category both then need recursive function under V1.php file.
-
mandeep-dhiman over 6 yearsI am having the same issue getting "Resource Not Found". anything still missing in implementation @BharatChodvadiya