ima9ines wrote:UPDATED (show separated category navigation in a product details page)
@using Nop.Core.Domain.Catalog
@using Nop.Core.Infrastructure
@using Nop.Services.Catalog
@model bool
@{
var categoryService = EngineContext.Current.Resolve<ICategoryService>();
var allCategories = categoryService.GetAllCategories();
var allLevel1Categories = allCategories.Where(i => i.ParentCategoryId == 0);
var onlyShowActiveHierarchy = Model;
//current category ID
var currentCategoryId = 0;
if (Url.RequestContext.RouteData.Values["controller"].ToString().Equals("catalog", StringComparison.InvariantCultureIgnoreCase) &&
Url.RequestContext.RouteData.Values["action"].ToString().Equals("category", StringComparison.InvariantCultureIgnoreCase))
{
currentCategoryId = Convert.ToInt32(Url.RequestContext.RouteData.Values["categoryId"].ToString());
}
//current product ID
var currentProductId = 0;
if (Url.RequestContext.RouteData.Values["controller"].ToString().Equals("product", StringComparison.InvariantCultureIgnoreCase) &&
Url.RequestContext.RouteData.Values["action"].ToString().Equals("productdetails", StringComparison.InvariantCultureIgnoreCase))
{
currentProductId = Convert.ToInt32(Url.RequestContext.RouteData.Values["productId"].ToString());
}
if (currentProductId > 0)
{
var productCategories = categoryService.GetProductCategoriesByProductId(currentProductId);
if (productCategories.Any())
{
currentCategoryId = productCategories[0].CategoryId;
}
}
}
@functions
{
public bool ContainsSelectedCategory(int selectedCategoryId, int ancestorCategoryId, IEnumerable<Category> allCategories)
{
if (selectedCategoryId == ancestorCategoryId)
{
return true;
}
var selectedCategory = allCategories.FirstOrDefault(i => i.Id == selectedCategoryId);
if (selectedCategory == null)
{
return false;
}
if (selectedCategory.ParentCategoryId == ancestorCategoryId)
{
return true;
}
return ContainsSelectedCategory(selectedCategory.ParentCategoryId, ancestorCategoryId, allCategories);
}
}
@helper RenderCategoryList(int selectedCategoryId, int parentCategoryId, bool generateSubCategories, IEnumerable<Category> allCategories)
{
var subCategories = EngineContext.Current.Resolve<ICategoryService>().GetAllCategoriesByParentCategoryId(parentCategoryId);
if (!subCategories.Any() || !(generateSubCategories || ContainsSelectedCategory(selectedCategoryId, parentCategoryId, allCategories)))
{
return;
}
<ul class="list">
@RenderCategoryLine(selectedCategoryId, subCategories, allCategories)
</ul>
}
@helper RenderCategoryLine(int selectedCategoryId, IEnumerable<Category> categories, IEnumerable<Category> allCategories)
{
var isSelected = false;
foreach (var category in categories)
{
isSelected = category.Id == selectedCategoryId;
<li class="@(isSelected ? "active" : "inactive")">
@RenderCategoryLink(category)
@RenderCategoryList(selectedCategoryId, category.Id, isSelected, allCategories)
</li>
}
}
@helper RenderCategoryLink(Category category)
{
<a href="@Url.RouteUrl("Category", new { SeName = category.ToModel().SeName })">@category.Name</a>
}
@if (allLevel1Categories.Any())
{
foreach (var category in allLevel1Categories)
{
if (onlyShowActiveHierarchy && !ContainsSelectedCategory(currentCategoryId, category.Id, allCategories))
{
continue;
}
<div class="block block-category-navigation">
<div class="title">
@RenderCategoryLink(category)
</div>
<div class="listbox">
@RenderCategoryList(currentCategoryId, category.Id, true, allCategories)
</div>
</div>
}
}
Hi Ima9ines
I just update to nopCommerce 3.9.
I using this category for home page. But I have a error as below. Can you help me!
Compiler Error Message: CS1061: 'Nop.Core.Domain.Catalog.Category' does not contain a definition for 'ToModel' and no extension method 'ToModel' accepting a first argument of type 'Nop.Core.Domain.Catalog.Category' could be found (are you missing a using directive or an assembly reference?)
Source Error:
Line 84: @helper RenderCategoryLink(Category category)
Line 85: {
Line 86: <a href="@Url.RouteUrl("Category", new { SeName = category.ToModel().SeName })">@category.Name</a>
Line 87: }
Line 88: @if (allLevel1Categories.Any())