0

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

enter image description here

enter image description here

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

Here is link to Download module : link

0

1 Answer 1

1

Please try this code

<?php
require_once('app/Mage.php');
Mage::app();

$yourCategory = Mage::getModel('ultimate_mars/category');
if(isset($_REQUEST["attributeCode"])){
    $categryCode = $_REQUEST["attributeCode"]; /* Your requested category code */
    $yourCategory = $yourCategory->load($categryCode,"producttype");
}
else{
    $yourCategory = $yourCategory->getCollection()->addFieldToFilter("parent_id",1)->getFirstItem();    
}
$childLevel = (int)$yourCategory->getData("categorylevel") + 1;
/* 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();
if($_categories->count() > 0){
    foreach($_categories as $_category)
    {
        $category[] = array("name" => $_category->getCategoryname(), "id" => $_category->getEntityId());
    }
}
else{
    $products = $yourCategory->getSelectedProductsCollection();

    if($products->count() > 0){
        foreach ($products as $product) {
            $category["products"][] = array("name" => $product->getProducttitle(), "id" => $product->getEntityId());
        }
    }
}
echo json_encode($category);

If found subcategory then return subcategory json, if not found any subcategory then it's return category products.

1
  • This is PeRFect , you Nailed it :-) Commented Nov 16, 2018 at 9:21

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.