Getting a Products Category

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
hypnotoad
Ensign (ENS)
Ensign (ENS)
Posts: 2
Joined: Sun Jul 11, 2010 11:07 am

Getting a Products Category

Post by hypnotoad » Sun Jul 11, 2010 11:16 am

Is there any way of getting a category by using the Product? I am having trouble figuring out how to bring back the product's category

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

Re: Getting a Products Category

Post by mazhar » Mon Jul 12, 2010 5:30 am

Well product object contains a member called Categories that returns the ids of parent categories for product. A product can be assigned to multiple categories that's why product.Categories returns list of ids.

If your product has only one parent then you can simply get the parent category via something like this

Code: Select all

Category category = CategoryDataSource.Load(product.Categories[0]);
If product belongs to multiple categories the perhaps you need to do the following in order get all parent categories

Code: Select all

foreach (int categoryId in product.Categories)
       {
           Category category = CategoryDataSource.Load(categoryId);
           //do the stuff with loaded category here
       }

Post Reply