Page 1 of 1

Accessing Product Categories list

Posted: Thu Oct 25, 2007 3:26 pm
by Will
On the product page, I want to display all of the categories a particular product belongs to.

I see there's a Product.Categories property. I can loop through it:

Code: Select all

#foreach($cf in $Product.Categories)

#end 
But I can't figure out the syntax to retrieve the name of each category as I loop through them. Having difficulty making sense of the rough API guide (as a non-programmer).

It should probably look something like:

Code: Select all

#foreach($cf in $Product.Categories)
     $cf.something
#end 
Just need to know the ".something".

Thanks.

Posted: Fri Oct 26, 2007 5:40 pm
by Logan Rhodehamel
The categories list a series of integers - IDs of the associated categories. Looking at it now, I think this is less than optimal but not something easily changed at this stage.

I can't find a way for that to be used effectively in nVelocity. It would probably require a simple custom control to do this.

Posted: Sat Oct 27, 2007 11:20 am
by Will
If we can list the id numbers, that would be okay as well. Going to use them to conditionally show chunks of html code. A number is as good as a name here.

Would we reference $cf.Category.Id (???)

So it would look something like...

#foreach($cf in $Product.Categories)
#if ($cf.Category.Id == 1)
<p>Category One Text</p>
#end
#if ($cf.Category.Id == 2)
<p>Category Two Text</p>
#end
#end

This would get the job done for us -- we've got pretty tight control over when categories are created in the catalog and are only interested in showing certain ones anyways.

Just need to know the syntax to get the category id number as we loop through.

Thanks.

Posted: Sat Oct 27, 2007 12:15 pm
by Logan Rhodehamel
Just use $cf - this is the integer ID of the category.

#foreach($cf in $Product.Categories)
#if ($cf == 1)
<p>Category One Text</p>
#end

Posted: Sat Oct 27, 2007 12:31 pm
by Will
Fantastic. Thanks!