Hi!
Can you make from subcategory 3,4... only show when current subcategory opening, like in default category.
Thank!
Computers (heading)
Desktops
All in one (subcategory 3- only show when opening)
PC
Notebooks
Accessories
Software & Games
Electronics (heading)
Camera, photo
Cell phones
Can you make from subcategory 3,4... only show when current subcategory opening, like in default category.
Thank!
Computers (heading)
Desktops
All in one (subcategory 3- only show when opening)
PC
Notebooks
Accessories
Software & Games
Electronics (heading)
Camera, photo
Cell phones
Here you are :).
I had code refactoring for it, please have a check (replace entire "_SeparatedCategoryNavigation.cshtml" file)
@using Nop.Core.Domain.Catalog
@using Nop.Core.Infrastructure
@using Nop.Services.Catalog
@{
//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());
}
var allCategories = EngineContext.Current.Resolve<ICategoryService>().GetAllCategories();
var allLevel1Categories = allCategories.Where(i => i.ParentCategoryId == 0);
}
@functions
{
public bool ContainsSelectedCategory(int selectedCategoryId, int ancestorCategoryId, IEnumerable<Category> allCategories)
{
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)
{
<div class="block block-category-navigation">
<div class="title">
@RenderCategoryLink(category)
</div>
<div class="listbox">
@RenderCategoryList(currentCategoryId, category.Id, true, allCategories)
</div>
</div>
}
}
Hope this help :)