Guest User

Untitled

a guest
Nov 16th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 KB | None | 0 0
  1. <?php
  2. require_once('app/Mage.php');
  3. Mage::app();
  4.  
  5. $yourCategory = Mage::getModel('ultimate_mars/category');
  6. if(isset($_REQUEST["attributeCode"])){
  7. $categryCode = $_REQUEST["attributeCode"]; /* category code */
  8. $yourCategory = $yourCategory->load($categryCode,"producttype");
  9. }
  10. else{
  11. $yourCategory = $yourCategory->getCollection()->addFieldToFilter("parent_id",1)->getFirstItem();
  12. }
  13. /* Getting subcategory of requested category code */
  14. $cat_model = Mage::getModel('ultimate_mars/category');
  15. $_categories = $cat_model->getTreeModel()->getCollection()->addFieldToFilter("parent_id",$yourCategory->getId());
  16.  
  17. $category = array();
  18. foreach($_categories as $_category)
  19. {
  20. $category[] = array("name" => $_category->getCategoryname(), "id" => $_category->getEntityId());
  21. }
  22.  
  23. echo json_encode($category);
  24.  
  25. <?php
  26.  
  27. class Ultimate_Mars_Model_Category extends Mage_Core_Model_Abstract
  28. {
  29.  
  30. const ENTITY = 'ultimate_mars_category';
  31. const CACHE_TAG = 'ultimate_mars_category';
  32.  
  33. protected $_eventPrefix = 'ultimate_mars_category';
  34.  
  35. protected $_eventObject = 'category';
  36.  
  37. public function _construct()
  38. {
  39. parent::_construct();
  40. $this->_init('ultimate_mars/category');
  41. }
  42.  
  43. public function getSelectedProductsCollection()
  44. {
  45. if (!$this->hasData('_product_collection')) {
  46. if (!$this->getId()) {
  47. return new Varien_Data_Collection();
  48. } else {
  49. $collection = Mage::getResourceModel('ultimate_mars/product_collection')
  50. ->addFieldToFilter('category_id', $this->getId());
  51. $this->setData('_product_collection', $collection);
  52. }
  53. }
  54. return $this->getData('_product_collection');
  55. }
  56.  
  57. public function getTreeModel()
  58. {
  59. return Mage::getResourceModel('ultimate_mars/category_tree');
  60. }
  61.  
  62. public function getTreeModelInstance()
  63. {
  64. if (is_null($this->_treeModel)) {
  65. $this->_treeModel = Mage::getResourceSingleton('ultimate_mars/category_tree');
  66. }
  67. return $this->_treeModel;
  68. }
  69.  
  70. public function getParentCategory()
  71. {
  72. if (!$this->hasData('parent_category')) {
  73. $this->setData(
  74. 'parent_category',
  75. Mage::getModel('ultimate_mars/category')->load($this->getParentId())
  76. );
  77. }
  78. return $this->_getData('parent_category');
  79. }
  80.  
  81. public function getParentId()
  82. {
  83. $parentIds = $this->getParentIds();
  84. return intval(array_pop($parentIds));
  85. }
  86.  
  87.  
  88. public function getParentIds()
  89. {
  90. return array_diff($this->getPathIds(), array($this->getId()));
  91. }
  92.  
  93.  
  94. public function getAllChildren($asArray = false)
  95. {
  96. $children = $this->getResource()->getAllChildren($this);
  97. if ($asArray) {
  98. return $children;
  99. } else {
  100. return implode(',', $children);
  101. }
  102. }
  103.  
  104. public function getChildCategorys()
  105. {
  106. return implode(',', $this->getResource()->getChildren($this, false));
  107. }
  108.  
  109.  
  110. public function checkId($id)
  111. {
  112. return $this->_getResource()->checkId($id);
  113. }
  114.  
  115.  
  116. public function getPathIds()
  117. {
  118. $ids = $this->getData('path_ids');
  119. if (is_null($ids)) {
  120. $ids = explode('/', $this->getPath());
  121. $this->setData('path_ids', $ids);
  122. }
  123. return $ids;
  124. }
  125.  
  126. public function getLevel()
  127. {
  128. if (!$this->hasLevel()) {
  129. return count(explode('/', $this->getPath())) - 1;
  130. }
  131. return $this->getData('level');
  132. }
  133.  
  134.  
  135. public function verifyIds(array $ids)
  136. {
  137. return $this->getResource()->verifyIds($ids);
  138. }
  139.  
  140. public function hasChildren()
  141. {
  142. return $this->_getResource()->getChildrenAmount($this) > 0;
  143. }
  144.  
  145. public function getCategorys($parent, $recursionLevel = 0, $sorted=false, $asCollection=false, $toLoad=true)
  146. {
  147. return $this->getResource()->getCategorys($parent, $recursionLevel, $sorted, $asCollection, $toLoad);
  148. }
  149.  
  150.  
  151. public function getParentCategorys()
  152. {
  153. return $this->getResource()->getParentCategorys($this);
  154. }
  155.  
  156.  
  157. public function getChildrenCategorys()
  158. {
  159. return $this->getResource()->getChildrenCategorys($this);
  160. }
  161.  
  162.  
  163. public function getStatusPath()
  164. {
  165. $parents = $this->getParentCategorys();
  166. $rootId = Mage::helper('ultimate_mars/category')->getRootCategoryId();
  167. foreach ($parents as $parent) {
  168. if ($parent->getId() == $rootId) {
  169. continue;
  170. }
  171. if (!$parent->getStatus()) {
  172. return false;
  173. }
  174. }
  175. return $this->getStatus();
  176. }
  177. }
  178.  
  179. <?php
  180. require_once('app/Mage.php');
  181. Mage::app();
  182.  
  183. $yourCategory = Mage::getModel('ultimate_mars/category');
  184. if(isset($_REQUEST["attributeCode"])){
  185. $categryCode = $_REQUEST["attributeCode"]; /* Your requested category code */
  186. $yourCategory = $yourCategory->load($categryCode,"producttype");
  187. }
  188. else{
  189. $yourCategory = $yourCategory->getCollection()->addFieldToFilter("parent_id",1)->getFirstItem();
  190. }
  191. $childLevel = (int)$yourCategory->getData("categorylevel") + 1;
  192. /* Getting subcategory of requested category code */
  193. $cat_model = Mage::getModel('ultimate_mars/category');
  194. $_categories = $cat_model->getTreeModel()->getCollection()->addFieldToFilter("parent_id",$yourCategory->getId());
  195.  
  196. $category = array();
  197. if($_categories->count() > 0){
  198. foreach($_categories as $_category)
  199. {
  200. $category[] = array("name" => $_category->getCategoryname(), "id" => $_category->getEntityId());
  201. }
  202. }
  203. else{
  204. $products = $yourCategory->getSelectedProductsCollection();
  205.  
  206. if($products->count() > 0){
  207. foreach ($products as $product) {
  208. $category["products"][] = array("name" => $product->getProducttitle(), "id" => $product->getEntityId());
  209. }
  210. }
  211. }
  212. echo json_encode($category);
Add Comment
Please, Sign In to add comment