Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

Add possibility to list all products with particular specification attribute on specification attribute edit page #208

KrzysztofPajak3fc68f094415
diff --git a/src/Business/Grand.Business.Catalog/Interfaces/Products/IProductService.cs b/src/Business/Grand.Business.Catalog/Interfaces/Products/IProductService.cs
index 14a0983ee..65b9492e1 100644
--- a/src/Business/Grand.Business.Catalog/Interfaces/Products/IProductService.cs
+++ b/src/Business/Grand.Business.Catalog/Interfaces/Products/IProductService.cs
@@ -130,6 +130,7 @@ namespace Grand.Business.Catalog.Interfaces.Products
/// <param name="searchProductTags">A value indicating whether to search by a specified "keyword" in product tags</param>
/// <param name="languageId">Language identifier (search for text searching)</param>
/// <param name="filteredSpecs">Filtered product specification identifiers</param>
+ /// <param name="specificationOptions">Specification options identifiers</param>
/// <param name="orderBy">Order by</param>
/// <param name="showHidden">A value indicating whether to show hidden records</param>
/// <param name="overridePublished">
@@ -162,6 +163,7 @@ namespace Grand.Business.Catalog.Interfaces.Products
bool searchProductTags = false,
string languageId = "",
IList<string> filteredSpecs = null,
+ IList<string> specificationOptions = null,
ProductSortingEnum orderBy = ProductSortingEnum.Position,
bool showHidden = false,
bool? overridePublished = null);
diff --git a/src/Business/Grand.Business.Catalog/Queries/Handlers/GetSearchProductsQueryHandler.cs b/src/Business/Grand.Business.Catalog/Queries/Handlers/GetSearchProductsQueryHandler.cs
index 35c8fe76f..e3cde22ec 100644
--- a/src/Business/Grand.Business.Catalog/Queries/Handlers/GetSearchProductsQueryHandler.cs
+++ b/src/Business/Grand.Business.Catalog/Queries/Handlers/GetSearchProductsQueryHandler.cs
@@ -235,6 +235,11 @@ namespace Grand.Business.Catalog.Queries.Handlers
}
}
+ if (request.SpecificationOptions != null && request.SpecificationOptions.Any())
+ {
+ query = query.Where(x => x.ProductSpecificationAttributes.Any(y => request.SpecificationOptions.Contains(y.SpecificationAttributeOptionId)));
+ }
+
if (request.OrderBy == ProductSortingEnum.Position && request.CategoryIds != null && request.CategoryIds.Any())
{
//category position
@@ -331,13 +336,13 @@ namespace Grand.Business.Catalog.Queries.Handlers
if (request.LoadFilterableSpecificationAttributeOptionIds && !_catalogSettings.IgnoreFilterableSpecAttributeOption)
{
IList<string> specyfication = new List<string>();
- var filterSpecExists = querySpecification.Where(x => x.ProductSpecificationAttributes.Any(x=>x.AllowFiltering));
+ var filterSpecExists = querySpecification.Where(x => x.ProductSpecificationAttributes.Any(x => x.AllowFiltering));
var qspec = from p in filterSpecExists
from item in p.ProductSpecificationAttributes
select item;
- var groupQuerySpec = qspec.Where(x=>x.AllowFiltering).GroupBy(x => new { SpecificationAttributeOptionId = x.SpecificationAttributeOptionId }).ToList();
+ var groupQuerySpec = qspec.Where(x => x.AllowFiltering).GroupBy(x => new { SpecificationAttributeOptionId = x.SpecificationAttributeOptionId }).ToList();
foreach (var item in groupQuerySpec)
{
specyfication.Add(item.Key.SpecificationAttributeOptionId);
diff --git a/src/Business/Grand.Business.Catalog/Queries/Models/GetSearchProductsQuery.cs b/src/Business/Grand.Business.Catalog/Queries/Models/GetSearchProductsQuery.cs
index ca826f94b..72e5224ec 100644
--- a/src/Business/Grand.Business.Catalog/Queries/Models/GetSearchProductsQuery.cs
+++ b/src/Business/Grand.Business.Catalog/Queries/Models/GetSearchProductsQuery.cs
@@ -33,6 +33,7 @@ namespace Grand.Business.Catalog.Queries.Handlers
public bool SearchProductTags { get; set; } = false;
public string LanguageId { get; set; } = "";
public IList<string> FilteredSpecs { get; set; } = null;
+ public IList<string> SpecificationOptions { get; set; } = null;
public ProductSortingEnum OrderBy { get; set; } = ProductSortingEnum.Position;
public bool ShowHidden { get; set; } = false;
public bool? OverridePublished { get; set; } = null;
diff --git a/src/Business/Grand.Business.Catalog/Services/Products/ProductService.cs b/src/Business/Grand.Business.Catalog/Services/Products/ProductService.cs
index 94201bd2e..0b8eb3ba6 100644
--- a/src/Business/Grand.Business.Catalog/Services/Products/ProductService.cs
+++ b/src/Business/Grand.Business.Catalog/Services/Products/ProductService.cs
@@ -452,6 +452,7 @@ namespace Grand.Business.Catalog.Services.Products
/// <param name="searchProductTags">A value indicating whether to search by a specified "keyword" in product tags</param>
/// <param name="languageId">Language identifier (search for text searching)</param>
/// <param name="filteredSpecs">Filtered product specification identifiers</param>
+ /// <param name="specificationOptions">Specification options identifiers</param>
/// <param name="orderBy">Order by</param>
/// <param name="showHidden">A value indicating whether to show hidden records</param>
/// <param name="overridePublished">
@@ -484,6 +485,7 @@ namespace Grand.Business.Catalog.Services.Products
bool searchProductTags = false,
string languageId = "",
IList<string> filteredSpecs = null,
+ IList<string> specificationOptions = null,
ProductSortingEnum orderBy = ProductSortingEnum.Position,
bool showHidden = false,
bool? overridePublished = null)
@@ -514,6 +516,7 @@ namespace Grand.Business.Catalog.Services.Products
SearchProductTags = searchProductTags,
LanguageId = languageId,
FilteredSpecs = filteredSpecs,
+ SpecificationOptions = specificationOptions,
OrderBy = orderBy,
ShowHidden = showHidden,
OverridePublished = overridePublished
diff --git a/src/Web/Grand.Web.Admin/Areas/Admin/Views/SpecificationAttribute/_CreateOrUpdate.TabOptions.cshtml b/src/Web/Grand.Web.Admin/Areas/Admin/Views/SpecificationAttribute/_CreateOrUpdate.TabOptions.cshtml
index 38f401a5d..00e53f943 100644
--- a/src/Web/Grand.Web.Admin/Areas/Admin/Views/SpecificationAttribute/_CreateOrUpdate.TabOptions.cshtml
+++ b/src/Web/Grand.Web.Admin/Areas/Admin/Views/SpecificationAttribute/_CreateOrUpdate.TabOptions.cshtml
@@ -92,6 +92,7 @@
confirmation: true,
mode: "inline"
},
+ autoBind: false,
scrollable: false,
columns: [{
field: "Name",
diff --git a/src/Web/Grand.Web.Admin/Areas/Admin/Views/SpecificationAttribute/_CreateOrUpdate.TabUsedByProducts.cshtml b/src/Web/Grand.Web.Admin/Areas/Admin/Views/SpecificationAttribute/_CreateOrUpdate.TabUsedByProducts.cshtml
new file mode 100644
index 000000000..ca005d5d6
--- /dev/null
+++ b/src/Web/Grand.Web.Admin/Areas/Admin/Views/SpecificationAttribute/_CreateOrUpdate.TabUsedByProducts.cshtml
@@ -0,0 +1,68 @@
+@model SpecificationAttributeModel
+@inject AdminAreaSettings adminAreaSettings
+@{
+ <div class="panel panel-default">
+ <vc:admin-widget widget-zone="specification_attribute_details_used_by_products_top" additional-data="Model" />
+ <div class="panel-body">
+ <div id="used-by-products-grid"></div>
+ </div>
+ <vc:admin-widget widget-zone="specification_attribute_details_used_by_products_bottom" additional-data="Model" />
+ </div>
+ <script>
+ $(document).ready(function () {
+ $("#used-by-products-grid").kendoGrid({
+ dataSource: {
+ transport: {
+ read: {
+ url: "@Html.Raw(Url.Action("UsedByProducts", "SpecificationAttribute", new { specificationAttributeId = Model.Id, area = Constants.AreaAdmin }))",
+ type: "POST",
+ dataType: "json",
+ data: addAntiForgeryToken
+ }
+ },
+ schema: {
+ data: "Data",
+ total: "Total",
+ errors: "Errors"
+ },
+ error: function(e) {
+ display_kendoui_grid_error(e);
+ // Cancel the changes
+ this.cancelChanges();
+ },
+ pageSize: @(adminAreaSettings.DefaultGridPageSize),
+ serverPaging: true,
+ serverFiltering: true,
+ serverSorting: true
+ },
+ pageable: {
+ refresh: true,
+ pageSizes: [@(adminAreaSettings.GridPageSizes)]
+ },
+ editable: {
+ confirmation: false,
+ mode: "inline"
+ },
+ autoBind: false,
+ scrollable: false,
+ columns: [{
+ field: "ProductName",
+ title: "@Loc["Admin.Catalog.Attributes.SpecificationAttributes.UsedByProducts.Product"]",
+ template: '<a class="k-link" href="@Url.Action("Edit", "Product", new { area = Constants.AreaAdmin })/#=Id#">#=kendo.htmlEncode(ProductName)#</a>',
+ },
+ {
+ field: "OptionName",
+ title: "@Loc["Admin.Catalog.Attributes.SpecificationAttributes.UsedByProducts.OptionName"]",
+ template: '<a class="k-link" href="@Url.Action("Edit", "Product", new { area = Constants.AreaAdmin })/#=Id#">#=kendo.htmlEncode(OptionName)#</a>',
+ },
+ {
+ field: "Published",
+ title: "@Loc["Admin.Catalog.Attributes.SpecificationAttributes.UsedByProducts.Published"]",
+ headerAttributes: { style: "text-align:center" },
+ attributes: { style: "text-align:center" },
+ template: '# if(Published) {# <i class="fa fa-check" aria-hidden="true" style="color:green"></i> #} else {# <i class="fa fa-times" aria-hidden="true" style="color:red"></i> #} #'
+ }]
+ });
+ });
+ </script>
+}
diff --git a/src/Web/Grand.Web.Admin/Areas/Admin/Views/SpecificationAttribute/_CreateOrUpdate.cshtml b/src/Web/Grand.Web.Admin/Areas/Admin/Views/SpecificationAttribute/_CreateOrUpdate.cshtml
index 493624cd5..f9582131f 100644
--- a/src/Web/Grand.Web.Admin/Areas/Admin/Views/SpecificationAttribute/_CreateOrUpdate.cshtml
+++ b/src/Web/Grand.Web.Admin/Areas/Admin/Views/SpecificationAttribute/_CreateOrUpdate.cshtml
@@ -2,7 +2,7 @@
<div asp-validation-summary="All"></div>
<input asp-for="Id" type="hidden" />
-<admin-tabstrip name="specificationattribute-edit">
+<admin-tabstrip name="specificationattribute-edit" BindGrid="true">
<items>
<tabstrip-item text="@Loc["Admin.Catalog.Attributes.SpecificationAttributes.Info"]" tab-index="0">
<content>
@@ -18,6 +18,16 @@
</div>
</content>
</tabstrip-item>
+ @if (!string.IsNullOrEmpty(Model.Id))
+ {
+ <tabstrip-item text="@Loc["Admin.Catalog.Attributes.SpecificationAttributes.UsedByProducts"]" tab-index="1">
+ <content>
+ <div>
+ <partial name="_CreateOrUpdate.TabUsedByProducts" model="Model" />
+ </div>
+ </content>
+ </tabstrip-item>
+ }
<tabstrip-item text="@Loc["Admin.Common.UserFields.Tab"]" tab-index="2">
<content>
<div>
diff --git a/src/Web/Grand.Web.Admin/Controllers/SpecificationAttributeController.cs b/src/Web/Grand.Web.Admin/Controllers/SpecificationAttributeController.cs
index 16d61f390..90fb7c75a 100644
--- a/src/Web/Grand.Web.Admin/Controllers/SpecificationAttributeController.cs
+++ b/src/Web/Grand.Web.Admin/Controllers/SpecificationAttributeController.cs
@@ -16,6 +16,7 @@ using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using System.Threading.Tasks;
+using System.Collections.Generic;
namespace Grand.Web.Admin.Controllers
{
@@ -25,6 +26,7 @@ namespace Grand.Web.Admin.Controllers
#region Fields
private readonly ISpecificationAttributeService _specificationAttributeService;
+ private readonly IProductService _productService;
private readonly ILanguageService _languageService;
private readonly ITranslationService _translationService;
private readonly ICustomerActivityService _customerActivityService;
@@ -44,6 +46,7 @@ namespace Grand.Web.Admin.Controllers
IStoreService storeService,
IWorkContext workContext,
IGroupService groupService,
+ IProductService productService,
SeoSettings seoSettings)
{
_specificationAttributeService = specificationAttributeService;
@@ -53,6 +56,7 @@ namespace Grand.Web.Admin.Controllers
_storeService = storeService;
_workContext = workContext;
_groupService = groupService;
+ _productService = productService;
_seoSettings = seoSettings;
}
@@ -71,8 +75,7 @@ namespace Grand.Web.Admin.Controllers
{
var specificationAttributes = await _specificationAttributeService
.GetSpecificationAttributes(command.Page - 1, command.PageSize);
- var gridModel = new DataSourceResult
- {
+ var gridModel = new DataSourceResult {
Data = specificationAttributes.Select(x => x.ToModel()),
Total = specificationAttributes.TotalCount
};
@@ -214,8 +217,7 @@ namespace Grand.Web.Admin.Controllers
public async Task<IActionResult> OptionList(string specificationAttributeId, DataSourceRequest command)
{
var options = (await _specificationAttributeService.GetSpecificationAttributeById(specificationAttributeId)).SpecificationAttributeOptions.OrderBy(x => x.DisplayOrder);
- var gridModel = new DataSourceResult
- {
+ var gridModel = new DataSourceResult {
Data = options.Select(x =>
{
var model = x.ToModel();
@@ -234,8 +236,7 @@ namespace Grand.Web.Admin.Controllers
[PermissionAuthorizeAction(PermissionActionName.Edit)]
public async Task<IActionResult> OptionCreatePopup(string specificationAttributeId)
{
- var model = new SpecificationAttributeOptionModel
- {
+ var model = new SpecificationAttributeOptionModel {
SpecificationAttributeId = specificationAttributeId
};
//locales
@@ -335,5 +336,66 @@ namespace Grand.Web.Admin.Controllers
return ErrorForKendoGridJson(ModelState);
}
#endregion
+
+ #region Used by products
+
+ //used by products
+ [PermissionAuthorizeAction(PermissionActionName.Preview)]
+ [HttpPost]
+ public async Task<IActionResult> UsedByProducts(DataSourceRequest command, string specificationAttributeId)
+ {
+ var specyfication = await _specificationAttributeService.GetSpecificationAttributeById(specificationAttributeId);
+ if (specyfication == null)
+ throw new ArgumentException("No specification found with the specified id");
+
+ var searchStoreId = string.Empty;
+
+ //limit for store manager
+ if (!string.IsNullOrEmpty(_workContext.CurrentCustomer.StaffStoreId))
+ searchStoreId = _workContext.CurrentCustomer.StaffStoreId;
+
+ var searchVendorId = string.Empty;
+ //a vendor should have access only to his products
+ if (_workContext.CurrentVendor != null)
+ {
+ searchVendorId = _workContext.CurrentVendor.Id;
+ }
+
+ var specificationProducts = new List<SpecificationAttributeModel.UsedByProductModel>();
+ var total = 0;
+
+ var searchspecificationOptions = specyfication.SpecificationAttributeOptions.Select(x => x.Id).ToList();
+ if (searchspecificationOptions.Any())
+ {
+ var products = (await _productService.SearchProducts(
+ storeId: searchStoreId,
+ vendorId: searchVendorId,
+ specificationOptions: searchspecificationOptions,
+ pageIndex: command.Page - 1,
+ pageSize: command.PageSize,
+ showHidden: true
+ )).products;
+
+ total = products.TotalCount;
+
+ foreach (var item in products)
+ {
+ var specOption = item.ProductSpecificationAttributes.FirstOrDefault(x => x.SpecificationAttributeId == specificationAttributeId);
+ specificationProducts.Add(new SpecificationAttributeModel.UsedByProductModel {
+ Id = item.Id,
+ ProductName = item.Name,
+ OptionName = specyfication.SpecificationAttributeOptions.FirstOrDefault(x => x.Id == specOption?.SpecificationAttributeOptionId)?.Name,
+ Published = item.Published
+ });
+ }
+ }
+ var gridModel = new DataSourceResult {
+ Data = specificationProducts,
+ Total = total
+ };
+ return Json(gridModel);
+ }
+
+ #endregion
}
}
diff --git a/src/Web/Grand.Web.Admin/Models/Catalog/SpecificationAttributeModel.cs b/src/Web/Grand.Web.Admin/Models/Catalog/SpecificationAttributeModel.cs
index 4204427ce..c552bedbc 100644
--- a/src/Web/Grand.Web.Admin/Models/Catalog/SpecificationAttributeModel.cs
+++ b/src/Web/Grand.Web.Admin/Models/Catalog/SpecificationAttributeModel.cs
@@ -1,9 +1,8 @@
-using Grand.Web.Common.Localization;
-using Grand.Infrastructure.ModelBinding;
+using Grand.Infrastructure.ModelBinding;
using Grand.Infrastructure.Models;
-using System.Collections.Generic;
-using Grand.Web.Common.Models;
using Grand.Web.Common.Link;
+using Grand.Web.Common.Models;
+using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Grand.Web.Admin.Models.Catalog
@@ -31,6 +30,15 @@ namespace Grand.Web.Admin.Models.Catalog
public IList<SpecificationAttributeLocalizedModel> Locales { get; set; }
+ public partial class UsedByProductModel : BaseEntityModel
+ {
+ [GrandResourceDisplayName("Admin.Catalog.Attributes.SpecificationAttributes.UsedByProducts.Product")]
+ public string ProductName { get; set; }
+ [GrandResourceDisplayName("Admin.Catalog.Attributes.SpecificationAttributes.UsedByProducts.OptionName")]
+ public string OptionName { get; set; }
+ [GrandResourceDisplayName("Admin.Catalog.Attributes.SpecificationAttributes.UsedByProducts.Published")]
+ public bool Published { get; set; }
+ }
}
public partial class SpecificationAttributeLocalizedModel : ILocalizedModelLocal
@@ -40,4 +48,6 @@ namespace Grand.Web.Admin.Models.Catalog
[GrandResourceDisplayName("Admin.Catalog.Attributes.SpecificationAttributes.Fields.Name")]
public string Name { get; set; }
}
+
+
}
\ No newline at end of file
diff --git a/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml b/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml
index 4b99a8127..e2e7e02b9 100644
--- a/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml
+++ b/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml
@@ -1956,6 +1956,18 @@
<Resource Name="Admin.Catalog.Attributes.SpecificationAttributes.Updated">
<Value>The attribute has been updated successfully.</Value>
</Resource>
+ <Resource Name="Admin.Catalog.Attributes.SpecificationAttributes.UsedByProducts">
+ <Value>Used by products</Value>
+ </Resource>
+ <Resource Name="Admin.Catalog.Attributes.SpecificationAttributes.UsedByProducts.OptionName">
+ <Value>Name</Value>
+ </Resource>
+ <Resource Name="Admin.Catalog.Attributes.SpecificationAttributes.UsedByProducts.Product">
+ <Value>Product</Value>
+ </Resource>
+ <Resource Name="Admin.Catalog.Attributes.SpecificationAttributes.UsedByProducts.Published">
+ <Value>Published</Value>
+ </Resource>
<Resource Name="Admin.Catalog.Brand.Imported">
<Value>Brands have been imported successfully.</Value>
</Resource>