getting category for product

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
RickSilver
Lieutenant (LT)
Lieutenant (LT)
Posts: 66
Joined: Mon Jun 22, 2009 5:49 pm

getting category for product

Post by RickSilver » Wed Feb 23, 2011 8:12 pm

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

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

Re: getting category for product

Post by mazhar » Thu Feb 24, 2011 5:25 am

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
        }

RickSilver
Lieutenant (LT)
Lieutenant (LT)
Posts: 66
Joined: Mon Jun 22, 2009 5:49 pm

Re: getting category for product

Post by RickSilver » Thu Feb 24, 2011 7:13 pm

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

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

Re: getting category for product

Post by mazhar » Fri Feb 25, 2011 4:24 am

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

RickSilver
Lieutenant (LT)
Lieutenant (LT)
Posts: 66
Joined: Mon Jun 22, 2009 5:49 pm

Re: getting category for product

Post by RickSilver » Fri Feb 25, 2011 4:30 pm

It is still coming back as TRUE regardless of the category visibility status as set in Admin.

Rick

plugables
Captain (CAPT)
Captain (CAPT)
Posts: 276
Joined: Sat Aug 15, 2009 4:04 am
Contact:

Re: getting category for product

Post by plugables » Thu Mar 10, 2011 9:44 am

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.

Post Reply