Developer forum

Forum » Templates » Get list of product categories in razor template

Get list of product categories in razor template

Kasper Birk Nielsen
Reply

Hi

 

 

How can I receive an array with all product categories in a razor template?

I need the product categories in the right order, and with category level  - 1 for main category, 2 for subcategory etc...

 

I just need the same output as displayed in the menu/navigation.

 


Replies

 
Mikkel Ricky
Reply
You can get the product categories (groups) from a given Shop using the Dynamicweb eCommerce API [http://developer.dynamicweb-cms.com/api8/#Dynamicweb~Dynamicweb_namespace.html].
 
Using a recursive Razor helper you can then render the groups in your template:
@{
	var shopID = "SHOP1";
	var langID = "LANG1";
	var shop = new Dynamicweb.eCommerce.Shops.Shop(shopID);
	<ul>
		@foreach (var group in shop.get_TopLevelGroups(langID)) {
		<li>
				@GroupTemplate(group, 0)
		</li>
		}
	</ul>
}

@helper GroupTemplate(Dynamicweb.eCommerce.Products.Group group, int level) {
	<div class="group">
		<div class="name">@group.Name (level: @level)</div>

		@if (group.Subgroups.Count > 0) {
		<ul>
			@foreach (var subgroup in group.Subgroups) {
			<li>@GroupTemplate(subgroup, level+1)</li>
			}
		</ul>
		}
	</div>
}
 
 
 
  

 

 
Kasper Birk Nielsen
Reply

Thanks Mikkel, this works fine :)

 
Mikkel Toustrup Olsen
Reply

I am about to try and re-write the abovementioned code in order for me to show x-amount of groups on the frontpage and then list their respective subgroups on a different page(Category). Is that possible to accomplish with the ecommerce module out of the box? - I am able to select specific groups on the frontpage - do I have to hardcode the "Category" page in the Group template in order for this to work? :)

 

You must be logged in to post in the forum