Background :
I am displaying sub-categories of category in Json format once we pass category's attributecode in url as in link : sitename.com/script.php?attributeCode=T_SHIRT
Note : I created categories & products using Ultimate Module Creator custom module
<?php
require_once('app/Mage.php');
Mage::app();
$yourCategory = Mage::getModel('ultimate_mars/category');
if(isset($_REQUEST["attributeCode"])){
$categryCode = $_REQUEST["attributeCode"]; /* category code */
$yourCategory = $yourCategory->load($categryCode,"producttype");
}
else{
$yourCategory = $yourCategory->getCollection()->addFieldToFilter("parent_id",1)->getFirstItem();
}
/* Getting subcategory of requested category code */
$cat_model = Mage::getModel('ultimate_mars/category');
$_categories = $cat_model->getTreeModel()->getCollection()->addFieldToFilter("parent_id",$yourCategory->getId());
$category = array();
foreach($_categories as $_category)
{
$category[] = array("name" => $_category->getCategoryname(), "id" => $_category->getEntityId());
}
echo json_encode($category);
Requirement :
But for some categories, there are no sub-categories, example : Men.... So In this case, i want to display Products of Men Category
means if i pass Men Category's attributeCode as link : sitename.com/script.php?attributeCode=MEN_T_SHIRT , than It should display its Products information (name, id) 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();
}
}