Category and sub-category API Magento
<?php
error_reporting(0);
include("../app/Mage.php"); //call Magento mage file
umask(0);
Mage::app();
$result_array = array();
$result_array_sub = array();
$_helper = Mage::helper('catalog/category');
$_categories = $_helper->getStoreCategories();
$currentCategory = Mage::registry('current_category');
if (count($_categories) > 0):
foreach($_categories as $_category):
$result_array_cat["category_name"]=$_category->getName();
$result_array_cat["categoryid"]=$categoryId=$_category->getId();
$_category1 = Mage::getModel('catalog/category')->load($categoryId);
$result_array_cat["status"]=$_category1->getIsActive();
$result_array_cat["description"]=htmlentities(preg_replace('/"/',' ',$_category1->getDescription()));
$result_array_cat["image"]=$_category1->getImageUrl();
$result_array_cat["count"]=$_category1->getProductCount();
$result_array_cat["subcategory_name"]=array();
$_category = Mage::getModel('catalog/category')->load($_category->getId());
$_subcategories = $_category->getChildrenCategories();
if (count($_subcategories) > 0):
foreach($_subcategories as $_subcategory):
$result_array_sub["sub_category_name"]=$_subcategory->getName();
$result_array_sub["sub_categoryid"]=$categoryId2=$_subcategory->getId();
$_category2 = Mage::getModel('catalog/category')->load($categoryId2);
$result_array_sub["status"]=$_category2->getIsActive();
$result_array_sub["description"]=htmlentities(preg_replace('/"/',' ',$_category2->getDescription()));
$result_array_sub["image"]=$_category2->getImageUrl();
$result_array_sub["count"]=$_category2->getProductCount();
array_push($result_array_cat["subcategory_name"], $result_array_sub);
endforeach;
endif;
array_push($result_array, $result_array_cat);
endforeach;
endif;
echo json_encode($result_array);
?>
No comments: