Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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);
- <?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();
- }
- }
- <?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);
Add Comment
Please, Sign In to add comment