Page 1 of 1

Getting a Products Category

Posted: Sun Jul 11, 2010 11:16 am
by hypnotoad
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

Re: Getting a Products Category

Posted: Mon Jul 12, 2010 5:30 am
by mazhar
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
       }