Page 1 of 1

getting category for product

Posted: Wed Feb 23, 2011 8:12 pm
by RickSilver
Given that I have an object of type Product, how can I get the category id so I can load a Category object. Basically, I'm just trying to get the Visibility of the category the product belongs in.

Rick

Re: getting category for product

Posted: Thu Feb 24, 2011 5:25 am
by mazhar
Product object has one property called Categories of type integer list. This property provides you the ids of categories to which product belongs.

Code: Select all

foreach (int categoryId in product.Categories)
        { 
            //USE categoryId here
        }

Re: getting category for product

Posted: Thu Feb 24, 2011 7:13 pm
by RickSilver
I am getting a Public value for a category that is hidden or private. Am i doing something wrong here?

foreach (int categoryId in product.Categories) {
Category _Category = CategoryDataSource.Load(categoryId);

if (_Category.Visibility.ToString() == "Public" ) {
// always true
}
}

Re: getting category for product

Posted: Fri Feb 25, 2011 4:24 am
by mazhar
Do it like this

Code: Select all

foreach (int categoryId in product.Categories)
        {
            Category category = CategoryDataSource.Load(categoryId);

            if (category.Visibility == CatalogVisibility.Public)
            {
                // always true
            }
        }

Re: getting category for product

Posted: Fri Feb 25, 2011 4:30 pm
by RickSilver
It is still coming back as TRUE regardless of the category visibility status as set in Admin.

Rick

Re: getting category for product

Posted: Thu Mar 10, 2011 9:44 am
by plugables
RickSilver wrote:It is still coming back as TRUE regardless of the category visibility status as set in Admin.
Make sure the product is not in more than one categories.