/** * Get and register categories root by specified categories IDs * * IDs can be arbitrary set of any categories ids. * Tree with minimal required nodes (all parents and neighbours) will be built. * If ids are empty, default tree with depth = 2 will be returned. * * @param array $ids * @return mixed */ public function getRootByIds($ids) { $root = $this->_coreRegistry->registry('root'); if (null === $root) { $ids = $this->_categoryTree->getExistingCategoryIdsBySpecifiedIds($ids); $tree = $this->_categoryTree->loadByIds($ids); $rootId = \Magento\Catalog\Model\Category::TREE_ROOT_ID; $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')); } $tree->addCollectionData($this->getCategoryCollection()); $this->_coreRegistry->register('root', $root); } return $root; }