Skip to content

Commit ec27c30

Browse files
fhebelthiennn
authored andcommitted
Initial Category Filter with SubCategories on CategoryDetail Page without localisation of intermediate breadcrumbs (simplcommerce#865, simplcommerce#546)
1 parent 6629ee8 commit ec27c30

File tree

3 files changed

+80
-5
lines changed

3 files changed

+80
-5
lines changed

src/Modules/SimplCommerce.Module.Catalog/Areas/Catalog/Components/CategoryBreadcrumbViewComponent.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@
66
using SimplCommerce.Infrastructure.Web;
77
using SimplCommerce.Module.Catalog.Models;
88
using SimplCommerce.Module.Core.Areas.Core.ViewModels;
9+
using SimplCommerce.Module.Core.Services;
910

1011
namespace SimplCommerce.Module.Catalog.Areas.Catalog.Components
1112
{
1213
public class CategoryBreadcrumbViewComponent : ViewComponent
1314
{
1415
private readonly IRepository<Category> _categoryRepository;
16+
private readonly IContentLocalizationService _contentLocalizationService;
1517

16-
public CategoryBreadcrumbViewComponent(IRepository<Category> categoryRepository)
18+
public CategoryBreadcrumbViewComponent(IRepository<Category> categoryRepository, IContentLocalizationService contentLocalizationService)
1719
{
1820
_categoryRepository = categoryRepository;
21+
_contentLocalizationService = contentLocalizationService;
1922
}
2023

2124
public IViewComponentResult Invoke(long? categoryId, IEnumerable<long> categoryIds)
@@ -44,7 +47,7 @@ private IList<BreadcrumbViewModel> Create(long categoryId)
4447
{
4548
new BreadcrumbViewModel
4649
{
47-
Text = category.Name,
50+
Text = _contentLocalizationService.GetLocalizedProperty(category, nameof(category.Name), category.Name),
4851
Url = category.Slug
4952
}
5053
};
@@ -53,7 +56,7 @@ private IList<BreadcrumbViewModel> Create(long categoryId)
5356
{
5457
breadcrumbModels.Insert(0, new BreadcrumbViewModel
5558
{
56-
Text = parentCategory.Name,
59+
Text = _contentLocalizationService.GetLocalizedProperty(parentCategory, nameof(parentCategory.Name), parentCategory.Name),
5760
Url = parentCategory.Slug
5861
});
5962
parentCategory = parentCategory.Parent;

src/Modules/SimplCommerce.Module.Catalog/Areas/Catalog/Controllers/CategoryController.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Linq;
1+
using System;
2+
using System.Linq;
23
using Microsoft.AspNetCore.Mvc;
34
using Microsoft.EntityFrameworkCore;
45
using Microsoft.Extensions.Configuration;
@@ -82,6 +83,12 @@ public IActionResult CategoryDetail(long id, SearchOption searchOption)
8283
query = query.Where(x => x.Price <= searchOption.MaxPrice.Value);
8384
}
8485

86+
var categories = searchOption.GetCategories();
87+
if (categories.Any())
88+
{
89+
query = query.Where(p => p.Categories.Select(c => c.CategoryId).Intersect(_categoryRepository.Query().Where(cat => categories.Contains(cat.Slug)).Select(c => c.Id)).Any());
90+
}
91+
8592
var brands = searchOption.GetBrands().ToArray();
8693
if (brands.Any())
8794
{
@@ -141,6 +148,24 @@ private static void AppendFilterOptionsToModel(ProductsByCategory model, IQuerya
141148
model.FilterOption.Price.MaxPrice = query.Max(x => x.Price);
142149
model.FilterOption.Price.MinPrice = query.Min(x => x.Price);
143150

151+
model.FilterOption.Categories = query
152+
.SelectMany(x => x.Categories)
153+
.GroupBy(x => new
154+
{
155+
x.Category.Id,
156+
x.Category.Name,
157+
x.Category.Slug,
158+
x.Category.ParentId
159+
})
160+
.Select(g => new FilterCategory
161+
{
162+
Id = (int)g.Key.Id,
163+
Name = g.Key.Name,
164+
Slug = g.Key.Slug,
165+
ParentId = g.Key.ParentId,
166+
Count = g.Count()
167+
}).ToList();
168+
144169
model.FilterOption.Brands = query.Include(x => x.Brand)
145170
.Where(x => x.BrandId != null).ToList()
146171
.GroupBy(x => x.Brand)

src/Modules/SimplCommerce.Module.Catalog/Areas/Catalog/Views/Category/CategoryDetail.cshtml

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,54 @@
2727
<div class="col-md-3 product-list-filters">
2828
<h3>@Localizer["Filter by"]</h3>
2929
<form id="productFilter" name="productFilter" method="GET" action="~/@Model.CategorySlug">
30-
<div id="accordion-brand">
30+
@if (Model.FilterOption.Categories.Where(x => x.ParentId == Model.CategoryId).Any())
31+
{
32+
<div id="accordion-category">
33+
<div class="card">
34+
<div class="card-header" id="cardHeaderCategory">
35+
<h5 class="mb-0">
36+
<a data-toggle="collapse" href="#collapse-category" aria-expanded="true" aria-controls="collapse-category">
37+
@Localizer["Category"]
38+
<i class="fa fa-angle-down"></i>
39+
</a>
40+
</h5>
41+
</div>
42+
<div id="collapse-category" class="collapse show" aria-labelledby="cardHeaderCategory" data-parent="#accordion-category">
43+
<div class="card-body">
44+
<ul class="list-unstyled checkbox-list">
45+
@foreach (var category in Model.FilterOption.Categories.Where(x => x.ParentId == Model.CategoryId))
46+
{
47+
<li>
48+
<label class="checkbox">
49+
<input type="checkbox" value="@category.Slug" name="category" checked="@Model.CurrentSearchOption.GetCategories().Contains(category.Slug)">
50+
@category.Name
51+
<small>(@category.Count)</small>
52+
</label>
53+
@{ var children = Model.FilterOption.Categories.Where(x => x.ParentId == category.Id); }
54+
@if (children.Any())
55+
{
56+
<ul class="list-unstyled checkbox-list">
57+
@foreach (var child in children)
58+
{
59+
<li>
60+
<label class="checkbox">
61+
<input type="checkbox" value="@child.Slug" name="category" checked="@Model.CurrentSearchOption.GetCategories().Contains(child.Slug)">
62+
@child.Name
63+
<small>(@child.Count)</small>
64+
</label>
65+
</li>
66+
}
67+
</ul>
68+
}
69+
</li>
70+
}
71+
</ul>
72+
</div>
73+
</div>
74+
</div>
75+
</div>
76+
}
77+
<div id="accordion-brand" class="mt-4">
3178
<div class="card">
3279
<div class="card-header" id="cardBrandHeader">
3380
<h5 class="mb-0">

0 commit comments

Comments
 (0)