I created Custom Category Tree using Ultimate Module Creator module by following github link
Magento Default Category Tree :
Ultimate Module Creator module Tree [ UMC ]:
I found that with help of below code we can display default categories in json format....
<?php
require_once('app/Mage.php');
$_helper = Mage::helper('catalog/category');
$_categories = $_helper->getStoreCategories();
$category = array();
foreach($_categories as $_category)
{
$_category = Mage::getModel('catalog/category')->load($_category->getId());
$category[] = array("name" => $_category->getName(), "id" => $_category->getId());
}
echo json_encode($category);
?>
Result :
Requirement :
but how to display custom categories [UMC] in json format ?
Custom categories are nothing but Entities in UMC module....
app/code/local/Ultimate/Mars/Model - Category.php
<?php
class Ultimate_Mars_Model_Category extends Mage_Core_Model_Abstract
{
/**
* Entity code.
* Can be used as part of method name for entity processing
*/
const ENTITY = 'ultimate_mars_category';
const CACHE_TAG = 'ultimate_mars_category';
/**
* Prefix of model events names
*
* @var string
*/
protected $_eventPrefix = 'ultimate_mars_category';
/**
* Parameter name in event
*
* @var string
*/
protected $_eventObject = 'category';
public function _construct()
{
parent::_construct();
$this->_init('ultimate_mars/category');
}
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 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 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 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();
}
}
app/code/local/Ultimate/Mars/Helper - Category.php
class Ultimate_Mars_Helper_Category extends Mage_Core_Helper_Abstract
{
const CATEGORY_ROOT_ID = 1;
/**
* get the root id
*
* @access public
* @return int
* @author Ultimate Module Creator
*/
public function getRootCategoryId()
{
return self::CATEGORY_ROOT_ID;
}
}
Update
I tried below code, but it did't worked for me....
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
header("Access-Control-Allow-Origin: *");
require_once('app/Mage.php');
umask(0);
Mage::app('default');
set_time_limit(0);
require_once('app/Mage.php');
$_rootId = Mage::helper('ultimate_mars/category');
$_categories = $_rootId->getCategorys();
$category = array();
foreach($_categories as $_category)
{
$_category = Mage::getModel('ultimate_mars/category')->getCategorys($_rootId);
$category[] = array("name" => $_category->getName(), "id" => $_category->getId());
}
echo json_encode($category);
?>
Error : Fatal error: Uncaught Error: Call to undefined method Ultimate_Mars_Helper_Category::getCategorys()