/** * {@inheritdoc} */ public function getTree($rootCategoryId = null, $depth = null) { $category = null; if ($rootCategoryId !== null) { /** @var \Magento\Catalog\Model\Category $category */ $category = $this->categoryRepository->get($rootCategoryId); } $result = $this->categoryTree->getTree($this->categoryTree->getRootNode($category), $depth); return $result; }
public function testGetRootNode() { $store = $this->getMockBuilder('Magento\\Store\\Model\\Store')->disableOriginalConstructor()->getMock(); $store->expects($this->once())->method('getRootCategoryId')->will($this->returnValue(2)); $store->expects($this->once())->method('getId')->will($this->returnValue(1)); $this->storeManagerMock->expects($this->any())->method('getStore')->will($this->returnValue($store)); $this->categoryCollection->expects($this->any())->method('addAttributeToSelect')->will($this->returnSelf()); $this->categoryCollection->expects($this->once())->method('setProductStoreId')->will($this->returnSelf()); $this->categoryCollection->expects($this->once())->method('setLoadProductCount')->will($this->returnSelf()); $this->categoryCollection->expects($this->once())->method('setStoreId')->will($this->returnSelf()); $node = $this->getMockBuilder('Magento\\Catalog\\Model\\ResourceModel\\Category\\Tree')->disableOriginalConstructor()->getMock(); $node->expects($this->once())->method('addCollectionData')->with($this->equalTo($this->categoryCollection)); $node->expects($this->once())->method('getNodeById')->with($this->equalTo(2)); $this->categoryTreeMock->expects($this->once())->method('load')->with($this->equalTo(null))->will($this->returnValue($node)); $this->tree->getRootNode(); }
/** * Get product category ids from array * * @param array $categories * @return array */ protected function getCategoryIds($categories) { $ids = []; $tree = $this->categoryTree->getTree($this->categoryTree->getRootNode(null), null); foreach ($categories as $name) { foreach ($tree->getChildrenData() as $child) { if ($child->getName() == $name) { /** @var \Magento\Catalog\Api\Data\CategoryTreeInterface $child */ $tree = $child; $ids[] = $child->getId(); if (!$tree->getChildrenData()) { $tree = $this->categoryTree->getTree($this->categoryTree->getRootNode(null), null); } break; } } } return $ids; }