Querying the Parent ID

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
mwolf
Lieutenant, Jr. Grade (LT JG)
Lieutenant, Jr. Grade (LT JG)
Posts: 50
Joined: Mon Jul 02, 2007 9:37 pm
Location: Chicago, IL
Contact:

Querying the Parent ID

Post by mwolf » Fri Jun 05, 2009 8:56 am

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?

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Querying the Parent ID

Post by mazhar » Fri Jun 05, 2009 9:31 am

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.

mwolf
Lieutenant, Jr. Grade (LT JG)
Lieutenant, Jr. Grade (LT JG)
Posts: 50
Joined: Mon Jul 02, 2007 9:37 pm
Location: Chicago, IL
Contact:

Re: Querying the Parent ID

Post by mwolf » Fri Jun 05, 2009 10:03 am

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?

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Querying the Parent ID

Post by mazhar » Fri Jun 05, 2009 10:20 am

Please check ConLib/CategoryBreadCrumbs control's code, that how it loads complete path of a products parents.

User avatar
jmestep
AbleCommerce Angel
Posts: 8164
Joined: Sun Feb 29, 2004 8:04 pm
Location: Dayton, OH
Contact:

Re: Querying the Parent ID

Post by jmestep » Fri Jun 05, 2009 10:38 am

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);
    }

Judy Estep
Web Developer
jestep@web2market.com
http://www.web2market.com
708-653-3100 x209
New search report plugin for business intelligence:
http://www.web2market.com/Search-Report ... -P154.aspx

Post Reply