4

I created Custom Category Tree using Ultimate Module Creator module by following github link

Magento Default Category Tree :

enter image description here

Ultimate Module Creator module Tree [ UMC ]:

enter image description here

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 :

enter image description here

Requirement :

but how to display custom categories [UMC] in json format ?

Custom categories are nothing but Entities in UMC module....

enter image description here

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()

Here is link to Download module : link

2
  • 2
    The function you are trying to call is from Ultimate_Mars_Helper_Category (Helper), while the function has been defined in the Model class. Please check and let me know if it helps. Commented Nov 13, 2018 at 14:00
  • 1
    @MohitKumarArora Thanks for support, i got solution..... Commented Nov 14, 2018 at 5:16

2 Answers 2

1
+100

Please use below code

<?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);
0
1

Add the code into your below helper file.

app/code/local/Ultimate/Mars/Helper - Category.php

class Ultimate_Mars_Helper_Category extends Mage_Core_Helper_Abstract
{
    const CATEGORY_ROOT_ID = 1;

    public function getRootCategoryId()
    {
        return self::CATEGORY_ROOT_ID;
    }

    public function getCategorys()
    {
        $_helper = Mage::helper('catalog/category');
        $_categories = $_helper->getStoreCategories();
        return $_categories;
    }

}
1
  • Thanks for your answer, i tried your code & i got Fatal error: Cannot declare class Ultimate_Mars_Helper_Category, because the name is already in use , other solution worked for me..... Commented Nov 14, 2018 at 5:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.