/** * @param \Magento\Catalog\Model\Category|null $category * @return Node|null */ public function getRootNode($category = null) { if ($category !== null && $category->getId()) { return $this->getNode($category); } $store = $this->storeManager->getStore(); $rootId = $store->getRootCategoryId(); $tree = $this->categoryTree->load(null); $this->prepareCollection(); $tree->addCollectionData($this->categoryCollection); $root = $tree->getNodeById($rootId); return $root; }
/** * @param mixed|null $parentNodeCategory * @param int $recursionLevel * @return Node|array|null * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function getRoot($parentNodeCategory = null, $recursionLevel = 3) { if ($parentNodeCategory !== null && $parentNodeCategory->getId()) { return $this->getNode($parentNodeCategory, $recursionLevel); } $root = $this->_coreRegistry->registry('root'); if ($root === null) { $storeId = (int) $this->getRequest()->getParam('store'); if ($storeId) { $store = $this->_storeManager->getStore($storeId); $rootId = $store->getRootCategoryId(); } else { $rootId = \Magento\Catalog\Model\Category::TREE_ROOT_ID; } $tree = $this->_categoryTree->load(null, $recursionLevel); if ($this->getCategory()) { $tree->loadEnsuredNodes($this->getCategory(), $tree->getNodeById($rootId)); } $tree->addCollectionData($this->getCategoryCollection()); $root = $tree->getNodeById($rootId); if ($root && $rootId != \Magento\Catalog\Model\Category::TREE_ROOT_ID) { $root->setIsVisible(true); } elseif ($root && $root->getId() == \Magento\Catalog\Model\Category::TREE_ROOT_ID) { $root->setName(__('Root')); } $this->_coreRegistry->register('root', $root); } return $root; }