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
getting category for product
-
- Lieutenant (LT)
- Posts: 66
- Joined: Mon Jun 22, 2009 5:49 pm
Re: getting category for product
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
}
-
- Lieutenant (LT)
- Posts: 66
- Joined: Mon Jun 22, 2009 5:49 pm
Re: getting category for product
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
}
}
foreach (int categoryId in product.Categories) {
Category _Category = CategoryDataSource.Load(categoryId);
if (_Category.Visibility.ToString() == "Public" ) {
// always true
}
}
Re: getting category for product
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
}
}
-
- Lieutenant (LT)
- Posts: 66
- Joined: Mon Jun 22, 2009 5:49 pm
Re: getting category for product
It is still coming back as TRUE regardless of the category visibility status as set in Admin.
Rick
Rick
Re: getting category for product
Make sure the product is not in more than one categories.RickSilver wrote:It is still coming back as TRUE regardless of the category visibility status as set in Admin.