Page 1 of 1

Querying the Parent ID

Posted: Fri Jun 05, 2009 8:56 am
by mwolf
When on a Product page, I'm trying to figure out what would be the correct way to query the true top level Parent_ID for a product. I'm currently using a query like below, which works, but doesn't always give me the correct result.

Code: Select all

SELECT cp.ParentId FROM ac_CatalogNodes cn INNER JOIN ac_CategoryParents cp ON cn.CategoryId = cp.CategoryId WHERE cn.CatalogNodeId = @productID AND cn.CatalogNodeTypeId = 1 AND cp.ParentLevel = 1
There seems to be an issue when a product is attached to multiple categories and it will sometimes return the wrong category_id. Is there any way I can modify my query to make sure that I'm getting the correct parent_id all of the time?

Re: Querying the Parent ID

Posted: Fri Jun 05, 2009 9:31 am
by mazhar
On product page if you reached product display page via navigating through its parent category then you should use following method to get category id

Code: Select all

int categoryId = PageHelper.GetCategoryId(true);
This method keep track of navigation path and returns exact parent category for product if it belongs to multiple categories.

Re: Querying the Parent ID

Posted: Fri Jun 05, 2009 10:03 am
by mwolf
That won't work because it doesn't return the top level parent id. See my example below:

Wedding & Anniversary (ID:5) > Cake Tops(ID:449) > Anniversary Cake Tops(ID:488)

PageHelper.GetCategoryId(true) would return Category ID 488 and I want it to return Category ID 5. Any other ideas?

Re: Querying the Parent ID

Posted: Fri Jun 05, 2009 10:20 am
by mazhar
Please check ConLib/CategoryBreadCrumbs control's code, that how it loads complete path of a products parents.

Re: Querying the Parent ID

Posted: Fri Jun 05, 2009 10:38 am
by jmestep
There is a CategoryParentDataSource class. I've used it like this before- I found it in the Admin/Catalog/MoveCatalogObject.aspx:

Code: Select all

protected void InitializeCategoryTree()
    {
        int st = 1;
        if(_CatalogNodeType == CatalogNodeType.Category) 
        {
            ListItem item = new ListItem("Top Level","0");
            NewPath.Items.Add(item);
            st = 0;
        }
        
        CategoryLevelNodeCollection categories = CategoryParentDataSource.GetCategoryLevels((_CatalogNodeType == CatalogNodeType.Category ? _CatalogNodeId : 0));
        foreach (CategoryLevelNode node in categories)
        {
            string prefix = string.Empty;
            for (int i = st; i <= node.CategoryLevel; i++) prefix += " . . ";
            NewPath.Items.Add(new ListItem(prefix + node.Name, node.CategoryId.ToString()));
            if (IsInExistingCategories(node.CategoryId))
            {
                // ADD TO CURRENT CATEGORIES LIST AS WELL
                CurrentCategories.Items.Add(new ListItem(node.Name, node.CategoryId.ToString()));
            }
        }
        //FIND CURRENT CATEGORY AND REMOVE
        ListItem current = NewPath.Items.FindByValue(_CategoryId.ToString());
        if (current != null) NewPath.SelectedIndex = NewPath.Items.IndexOf(current);
    }