Search code examples
atg

View product category tree (hierarchical view)


I have products existing in multiple categories.

Example:

Category > Books > Hardcover Books > childCategory
Category > Promo Books > Sample > childCategory

Are there any helper classes that can be used to get the list of all the categories for a product?


Solution

  • I'm not aware of any utility classes OOTB that will provide you with a full tree structure for a product. That said, it is not too difficult to build this up.

    Given a productId and, assuming you know the 'root' categoryId, add the following methods to your extension of the CatalogTools class.

        private RqlStatement findParentCategoriesOfProductRQL;
    
        public RepositoryItem[] findParentCategoriesOfProduct(String productId, String rootCategoryId) throws RepositoryException {
            RepositoryView view = getCatalog().getView("category");
            RepositoryItem[] parentCategories = findParentCategoriesOfProductRQL.executeQuery(view, new Object[] {productId, rootCategoryId });
            if (parentCategories == null || parentCategories.length == 0) {
                return null;
            }
            return parentCategories;
        }
    
        public void setFindParentCategoriesOfProductRQL(RqlStatement findParentCategoriesOfProduct) {
            this.findParentCategoriesOfProductRQL = findParentCategoriesOfProduct;
        }
    
        public RqlStatement getFindParentCategoriesOfProductRQL() {
            return findParentCategoriesOfProductRQL;
        }
    

    and add the following to your CatalogTools.properties

    findParentCategoriesOfProductRQL=fixedChildProducts includes item (id = ?0) AND ancestorCategoryIds includes ?1