I am displaying all Custom Categories & its sub-categories in Json format with help of below code....
Note : These Categories & its sub-categories are created using custom module
<?php
require_once('app/Mage.php');
Mage::app();
$cat_model = Mage::getModel('ultimate_mars/category');
$_categories = $cat_model->getTreeModel()->getCollection()->addFieldToFilter("entity_id", array('gt' => 1));
$category = array();
foreach($_categories as $_category)
{
$category[] = array("name" => $_category->getCategoryname(), "id" => $_category->getEntityId());
}
echo json_encode($category);
Result :
What i need :
If i pass particular category id [say Tshirts id] in Url, than i need to display only its sub-categories [ say Men & Women ] in JSON format....
app/code/local/Ultimate/Mars/Model - Category.php
<?php
class Ultimate_Mars_Model_Category extends Mage_Core_Model_Abstract
{
const ENTITY = 'ultimate_mars_category';
const CACHE_TAG = 'ultimate_mars_category';
protected $_eventPrefix = 'ultimate_mars_category';
protected $_eventObject = 'category';
public function _construct()
{
parent::_construct();
$this->_init('ultimate_mars/category');
}
public function getSelectedProductsCollection()
{
if (!$this->hasData('_product_collection')) {
if (!$this->getId()) {
return new Varien_Data_Collection();
} else {
$collection = Mage::getResourceModel('ultimate_mars/product_collection')
->addFieldToFilter('category_id', $this->getId());
$this->setData('_product_collection', $collection);
}
}
return $this->getData('_product_collection');
}
public function getTreeModel()
{
return Mage::getResourceModel('ultimate_mars/category_tree');
}
public function getTreeModelInstance()
{
if (is_null($this->_treeModel)) {
$this->_treeModel = Mage::getResourceSingleton('ultimate_mars/category_tree');
}
return $this->_treeModel;
}
public function getParentCategory()
{
if (!$this->hasData('parent_category')) {
$this->setData(
'parent_category',
Mage::getModel('ultimate_mars/category')->load($this->getParentId())
);
}
return $this->_getData('parent_category');
}
public function getParentId()
{
$parentIds = $this->getParentIds();
return intval(array_pop($parentIds));
}
public function getParentIds()
{
return array_diff($this->getPathIds(), array($this->getId()));
}
public function getAllChildren($asArray = false)
{
$children = $this->getResource()->getAllChildren($this);
if ($asArray) {
return $children;
} else {
return implode(',', $children);
}
}
public function getChildCategorys()
{
return implode(',', $this->getResource()->getChildren($this, false));
}
public function checkId($id)
{
return $this->_getResource()->checkId($id);
}
public function getPathIds()
{
$ids = $this->getData('path_ids');
if (is_null($ids)) {
$ids = explode('/', $this->getPath());
$this->setData('path_ids', $ids);
}
return $ids;
}
public function getLevel()
{
if (!$this->hasLevel()) {
return count(explode('/', $this->getPath())) - 1;
}
return $this->getData('level');
}
public function verifyIds(array $ids)
{
return $this->getResource()->verifyIds($ids);
}
public function hasChildren()
{
return $this->_getResource()->getChildrenAmount($this) > 0;
}
public function getCategorys($parent, $recursionLevel = 0, $sorted=false, $asCollection=false, $toLoad=true)
{
return $this->getResource()->getCategorys($parent, $recursionLevel, $sorted, $asCollection, $toLoad);
}
public function getParentCategorys()
{
return $this->getResource()->getParentCategorys($this);
}
public function getChildrenCategorys()
{
return $this->getResource()->getChildrenCategorys($this);
}
public function getStatusPath()
{
$parents = $this->getParentCategorys();
$rootId = Mage::helper('ultimate_mars/category')->getRootCategoryId();
foreach ($parents as $parent) {
if ($parent->getId() == $rootId) {
continue;
}
if (!$parent->getStatus()) {
return false;
}
}
return $this->getStatus();
}
}