/** * Retrieve categories * * @param integer $parent * @param integer $recursionLevel * @param boolean|string $sorted * @param boolean $asCollection * @param boolean $toLoad * @return \Magento\Framework\Data\Tree\Node\Collection|\Magento\Catalog\Model\Resource\Category\Collection */ public function getCategories($parent, $recursionLevel = 0, $sorted = false, $asCollection = false, $toLoad = true) { $tree = $this->_categoryTreeFactory->create(); /* @var $tree \Magento\Catalog\Model\Resource\Category\Tree */ $nodes = $tree->loadNode($parent)->loadChildren($recursionLevel)->getChildren(); $tree->addCollectionData(null, $sorted, $parent, $toLoad, true); if ($asCollection) { return $tree->getCollection(); } return $nodes; }
/** * Get category tree * * @param int|null $rootNode * @param bool $reload * @return \Magento\Framework\Data\Tree\Node */ protected function getTree($rootNode = null, $reload = false) { if (!$this->categoryTree || $reload) { if ($rootNode === null) { $rootNode = $this->storeManager->getStore()->getRootCategoryId(); } $tree = $this->resourceCategoryTreeFactory->create(); $node = $tree->loadNode($rootNode)->loadChildren(); $tree->addCollectionData(null, false, $rootNode); $this->categoryTree = $node; } return $this->categoryTree; }
/** * Retrieve category tree model * * @return \Magento\Catalog\Model\Resource\Category\Tree */ public function getTreeModel() { return $this->_categoryTreeFactory->create(); }