/** * Find a category from its id * * @param int $categoryId * * @throws NotFoundHttpException * * @return CategoryInterface */ protected function findCategory($categoryId) { $category = $this->categoryRepository->find($categoryId); if (!$category) { throw $this->createNotFoundException('Category not found'); } return $category; }
/** * Get children category ids * * @param integer[] $categoryIds * * @return integer[] */ protected function getAllChildrenIds(array $categoryIds) { $allChildrenIds = []; foreach ($categoryIds as $categoryId) { $category = $this->categoryRepository->find($categoryId); $childrenIds = $this->categoryRepository->getAllChildrenIds($category); $childrenIds[] = $category->getId(); $allChildrenIds = array_merge($allChildrenIds, $childrenIds); } return $allChildrenIds; }
/** * Add filter to display categorized products * * @param FilterDatasourceAdapterInterface $ds * @param array $data * * @return bool has been applied */ protected function applyFilterByCategory(FilterDatasourceAdapterInterface $ds, $data) { $category = $this->categoryRepo->find($data['categoryId']); if (!$category) { $category = $this->categoryRepo->find($data['treeId']); } if ($category) { if ($data['includeSub']) { $categoryIds = $this->getAllChildrenIds($category); } else { $categoryIds = array(); } $categoryIds[] = $category->getId(); $this->util->applyFilter($ds, 'categories.id', 'IN', $categoryIds); return true; } return false; }