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 new feature on the catalog page - Allow sorting by rating

support3cc13ed8a489
diff --git a/src/Business/Grand.Business.Catalog/Commands/UpdateProductReviewTotalsCommandHandler.cs b/src/Business/Grand.Business.Catalog/Commands/UpdateProductReviewTotalsCommandHandler.cs
index dc3c3e28b..1cebd332e 100644
--- a/src/Business/Grand.Business.Catalog/Commands/UpdateProductReviewTotalsCommandHandler.cs
+++ b/src/Business/Grand.Business.Catalog/Commands/UpdateProductReviewTotalsCommandHandler.cs
@@ -18,7 +18,8 @@ namespace Grand.Business.Catalog.Commands.Handlers
#endregion
- public UpdateProductReviewTotalsCommandHandler(IRepository<Product> productRepository, IProductReviewService productReviewService, ICacheBase cacheBase)
+ public UpdateProductReviewTotalsCommandHandler(IRepository<Product> productRepository,
+ IProductReviewService productReviewService, ICacheBase cacheBase)
{
_productRepository = productRepository;
_cacheBase = cacheBase;
@@ -56,14 +57,16 @@ namespace Grand.Business.Catalog.Commands.Handlers
request.Product.NotApprovedRatingSum = notApprovedRatingSum;
request.Product.ApprovedTotalReviews = approvedTotalReviews;
request.Product.NotApprovedTotalReviews = notApprovedTotalReviews;
-
+ request.Product.AvgRating = approvedTotalReviews > 0 ? Math.Round((double)approvedRatingSum / approvedTotalReviews, 2) : 0;
+
var update = UpdateBuilder<Product>.Create()
- .Set(x => x.ApprovedRatingSum, request.Product.ApprovedRatingSum)
- .Set(x => x.NotApprovedRatingSum, request.Product.NotApprovedRatingSum)
- .Set(x => x.ApprovedTotalReviews, request.Product.ApprovedTotalReviews)
- .Set(x => x.NotApprovedTotalReviews, request.Product.NotApprovedTotalReviews);
+ .Set(x => x.ApprovedRatingSum, request.Product.ApprovedRatingSum)
+ .Set(x => x.NotApprovedRatingSum, request.Product.NotApprovedRatingSum)
+ .Set(x => x.ApprovedTotalReviews, request.Product.ApprovedTotalReviews)
+ .Set(x => x.AvgRating, request.Product.AvgRating)
+ .Set(x => x.NotApprovedTotalReviews, request.Product.NotApprovedTotalReviews);
- await _productRepository.UpdateOneAsync(x=>x.Id == request.Product.Id, update);
+ await _productRepository.UpdateOneAsync(x => x.Id == request.Product.Id, update);
//cache
await _cacheBase.RemoveByPrefix(string.Format(CacheKey.PRODUCTS_BY_ID_KEY, request.Product.Id));
@@ -71,4 +74,4 @@ namespace Grand.Business.Catalog.Commands.Handlers
return true;
}
}
-}
+}
\ No newline at end of file
diff --git a/src/Business/Grand.Business.Catalog/Queries/Handlers/GetSearchProductsQueryHandler.cs b/src/Business/Grand.Business.Catalog/Queries/Handlers/GetSearchProductsQueryHandler.cs
index fb93bbcf5..39808069a 100644
--- a/src/Business/Grand.Business.Catalog/Queries/Handlers/GetSearchProductsQueryHandler.cs
+++ b/src/Business/Grand.Business.Catalog/Queries/Handlers/GetSearchProductsQueryHandler.cs
@@ -9,9 +9,9 @@ using MediatR;
namespace Grand.Business.Catalog.Queries.Handlers
{
- public class GetSearchProductsQueryHandler : IRequestHandler<GetSearchProductsQuery, (IPagedList<Product> products, IList<string> filterableSpecificationAttributeOptionIds)>
+ public class GetSearchProductsQueryHandler : IRequestHandler<GetSearchProductsQuery, (IPagedList<Product> products,
+ IList<string> filterableSpecificationAttributeOptionIds)>
{
-
private readonly IRepository<Product> _productRepository;
private readonly ISpecificationAttributeService _specificationAttributeService;
@@ -43,40 +43,42 @@ namespace Grand.Business.Catalog.Queries.Handlers
//products
var query = from p in _productRepository.Table
- select p;
-
+ select p;
+
//category filtering
if (request.CategoryIds != null && request.CategoryIds.Any())
{
-
if (request.FeaturedProducts.HasValue)
{
query = query.Where(x => x.ProductCategories.Any(y => request.CategoryIds.Contains(y.CategoryId)
- && y.IsFeaturedProduct == request.FeaturedProducts));
+ && y.IsFeaturedProduct ==
+ request.FeaturedProducts));
}
else
{
query = query.Where(x => x.ProductCategories.Any(y => request.CategoryIds.Contains(y.CategoryId)));
}
}
+
//brand
if (!string.IsNullOrEmpty(request.BrandId))
{
query = query.Where(x => x.BrandId == request.BrandId);
}
+
//collection filtering
if (!string.IsNullOrEmpty(request.CollectionId))
{
if (request.FeaturedProducts.HasValue)
{
query = query.Where(x => x.ProductCollections.Any(y => y.CollectionId == request.CollectionId
- && y.IsFeaturedProduct == request.FeaturedProducts));
+ && y.IsFeaturedProduct ==
+ request.FeaturedProducts));
}
else
{
query = query.Where(x => x.ProductCollections.Any(y => y.CollectionId == request.CollectionId));
}
-
}
if (!request.OverridePublished.HasValue)
@@ -97,19 +99,23 @@ namespace Grand.Business.Catalog.Queries.Handlers
//unpublished only
query = query.Where(p => !p.Published);
}
+
if (request.VisibleIndividuallyOnly)
{
query = query.Where(p => p.VisibleIndividually);
}
+
if (request.ProductType.HasValue)
{
var productTypeId = (int)request.ProductType.Value;
query = query.Where(p => p.ProductTypeId == (ProductType)productTypeId);
}
+
if (request.ShowOnHomePage.HasValue)
{
query = query.Where(p => p.ShowOnHomePage == request.ShowOnHomePage.Value);
}
+
//The function 'CurrentUtcDateTime' is not supported by SQL Server Compact.
//That's why we pass the date value
var nowUtc = DateTime.UtcNow;
@@ -117,11 +123,13 @@ namespace Grand.Business.Catalog.Queries.Handlers
{
query = query.Where(p => p.Price >= request.PriceMin.Value);
}
+
if (request.PriceMax.HasValue)
{
//max price
query = query.Where(p => p.Price <= request.PriceMax.Value);
}
+
if (!request.ShowHidden && !_catalogSettings.IgnoreFilterableAvailableStartEndDateTime)
{
query = query.Where(p =>
@@ -140,55 +148,58 @@ namespace Grand.Business.Catalog.Queries.Handlers
//searching by keyword
if (!string.IsNullOrWhiteSpace(request.Keywords))
{
-
if (!request.SearchDescriptions)
query = query.Where(p =>
p.Name.ToLower().Contains(request.Keywords.ToLower())
||
- p.Locales.Any(x => x.LocaleKey == "Name" && x.LocaleValue != null && x.LocaleValue.ToLower().Contains(request.Keywords.ToLower()))
+ p.Locales.Any(x =>
+ x.LocaleKey == "Name" && x.LocaleValue != null &&
+ x.LocaleValue.ToLower().Contains(request.Keywords.ToLower()))
||
(request.SearchSku && p.Sku != null && p.Sku.ToLower().Contains(request.Keywords.ToLower()))
- );
+ );
else
{
query = query.Where(p =>
- (p.Name != null && p.Name.ToLower().Contains(request.Keywords.ToLower()))
- ||
- (p.ShortDescription != null && p.ShortDescription.ToLower().Contains(request.Keywords.ToLower()))
- ||
- (p.FullDescription != null && p.FullDescription.ToLower().Contains(request.Keywords.ToLower()))
- ||
- p.Locales.Any(x => x.LocaleValue != null && x.LocaleValue.ToLower().Contains(request.Keywords.ToLower()))
- ||
- (request.SearchSku && p.Sku != null && p.Sku.ToLower().Contains(request.Keywords.ToLower()))
- );
+ (p.Name != null && p.Name.ToLower().Contains(request.Keywords.ToLower()))
+ ||
+ (p.ShortDescription != null &&
+ p.ShortDescription.ToLower().Contains(request.Keywords.ToLower()))
+ ||
+ (p.FullDescription != null && p.FullDescription.ToLower().Contains(request.Keywords.ToLower()))
+ ||
+ p.Locales.Any(x =>
+ x.LocaleValue != null && x.LocaleValue.ToLower().Contains(request.Keywords.ToLower()))
+ ||
+ (request.SearchSku && p.Sku != null && p.Sku.ToLower().Contains(request.Keywords.ToLower()))
+ );
}
-
-
}
if (!request.ShowHidden && !CommonHelper.IgnoreAcl)
{
query = from p in query
- where !p.LimitedToGroups || allowedCustomerGroupsIds.Any(x => p.CustomerGroups.Contains(x))
- select p;
+ where !p.LimitedToGroups || allowedCustomerGroupsIds.Any(x => p.CustomerGroups.Contains(x))
+ select p;
}
if (!string.IsNullOrEmpty(request.StoreId) && !CommonHelper.IgnoreStoreLimitations)
{
query = query.Where(x => x.Stores.Any(y => y == request.StoreId) || !x.LimitedToStores);
-
}
+
//vendor filtering
if (!string.IsNullOrEmpty(request.VendorId))
{
query = query.Where(x => x.VendorId == request.VendorId);
}
+
//warehouse filtering
if (!string.IsNullOrEmpty(request.WarehouseId))
{
query = query.Where(x =>
- (x.UseMultipleWarehouses && x.ProductWarehouseInventory.Any(y => y.WarehouseId == request.WarehouseId))
+ (x.UseMultipleWarehouses &&
+ x.ProductWarehouseInventory.Any(y => y.WarehouseId == request.WarehouseId))
|| (!x.UseMultipleWarehouses && x.WarehouseId == request.WarehouseId));
}
@@ -214,100 +225,112 @@ namespace Grand.Business.Catalog.Queries.Handlers
{
//add
dictionary.Add(specification.Id, new List<string>());
- querySpecification = querySpecification.Where(x => x.ProductSpecificationAttributes.Any(y => y.SpecificationAttributeId == specification.Id && y.AllowFiltering));
+ querySpecification = querySpecification.Where(x =>
+ x.ProductSpecificationAttributes.Any(y =>
+ y.SpecificationAttributeId == specification.Id && y.AllowFiltering));
}
+
dictionary[specification.Id].Add(key);
}
foreach (var item in dictionary)
{
- query = query.Where(x => x.ProductSpecificationAttributes.Any(y => y.SpecificationAttributeId == item.Key && y.AllowFiltering
- && item.Value.Contains(y.SpecificationAttributeOptionId)));
+ query = query.Where(x => x.ProductSpecificationAttributes.Any(y =>
+ y.SpecificationAttributeId == item.Key && y.AllowFiltering
+ && item.Value.Contains(
+ y.SpecificationAttributeOptionId)));
}
}
if (request.SpecificationOptions != null && request.SpecificationOptions.Any())
{
- query = query.Where(x => x.ProductSpecificationAttributes.Any(y => request.SpecificationOptions.Contains(y.SpecificationAttributeOptionId)));
+ query = query.Where(x => x.ProductSpecificationAttributes.Any(y =>
+ request.SpecificationOptions.Contains(y.SpecificationAttributeOptionId)));
}
switch (request.OrderBy)
{
case ProductSortingEnum.Position when request.CategoryIds != null && request.CategoryIds.Any():
//category position
- query = _catalogSettings.SortingByAvailability ?
- query.OrderBy(x => x.LowStock).ThenBy(x => x.DisplayOrderCategory) :
- query.OrderBy(x => x.DisplayOrderCategory);
+ query = _catalogSettings.SortingByAvailability
+ ? query.OrderBy(x => x.LowStock).ThenBy(x => x.DisplayOrderCategory)
+ : query.OrderBy(x => x.DisplayOrderCategory);
break;
case ProductSortingEnum.Position when !string.IsNullOrEmpty(request.BrandId):
//brand position
- query = _catalogSettings.SortingByAvailability ?
- query.OrderBy(x => x.LowStock).ThenBy(x => x.DisplayOrderBrand) :
- query.OrderBy(x => x.DisplayOrderBrand);
+ query = _catalogSettings.SortingByAvailability
+ ? query.OrderBy(x => x.LowStock).ThenBy(x => x.DisplayOrderBrand)
+ : query.OrderBy(x => x.DisplayOrderBrand);
break;
case ProductSortingEnum.Position when !string.IsNullOrEmpty(request.CollectionId):
//collection position
- query = _catalogSettings.SortingByAvailability ?
- query.OrderBy(x => x.LowStock).ThenBy(x => x.DisplayOrderCollection) :
- query.OrderBy(x => x.DisplayOrderCollection);
+ query = _catalogSettings.SortingByAvailability
+ ? query.OrderBy(x => x.LowStock).ThenBy(x => x.DisplayOrderCollection)
+ : query.OrderBy(x => x.DisplayOrderCollection);
break;
case ProductSortingEnum.Position:
//otherwise sort by name
- query = _catalogSettings.SortingByAvailability ?
- query.OrderBy(x => x.LowStock).ThenBy(x => x.Name) :
- query.OrderBy(x => x.Name);
+ query = _catalogSettings.SortingByAvailability
+ ? query.OrderBy(x => x.LowStock).ThenBy(x => x.Name)
+ : query.OrderBy(x => x.Name);
break;
case ProductSortingEnum.NameAsc:
//Name: A to Z
- query = _catalogSettings.SortingByAvailability ?
- query.OrderBy(x => x.LowStock).ThenBy(x => x.Name) :
- query.OrderBy(x => x.Name);
+ query = _catalogSettings.SortingByAvailability
+ ? query.OrderBy(x => x.LowStock).ThenBy(x => x.Name)
+ : query.OrderBy(x => x.Name);
break;
case ProductSortingEnum.NameDesc:
//Name: Z to A
- query = _catalogSettings.SortingByAvailability ?
- query.OrderBy(x => x.LowStock).ThenByDescending(x => x.Name) :
- query.OrderByDescending(x => x.Name);
+ query = _catalogSettings.SortingByAvailability
+ ? query.OrderBy(x => x.LowStock).ThenByDescending(x => x.Name)
+ : query.OrderByDescending(x => x.Name);
break;
case ProductSortingEnum.PriceAsc:
//Price: Low to High
- query = _catalogSettings.SortingByAvailability ?
- query.OrderBy(x => x.LowStock).ThenBy(x => x.Price) :
- query.OrderBy(x => x.Price);
+ query = _catalogSettings.SortingByAvailability
+ ? query.OrderBy(x => x.LowStock).ThenBy(x => x.Price)
+ : query.OrderBy(x => x.Price);
break;
case ProductSortingEnum.PriceDesc:
//Price: High to Low
- query = _catalogSettings.SortingByAvailability ?
- query.OrderBy(x => x.LowStock).ThenByDescending(x => x.Price) :
- query.OrderByDescending(x => x.Price);
+ query = _catalogSettings.SortingByAvailability
+ ? query.OrderBy(x => x.LowStock).ThenByDescending(x => x.Price)
+ : query.OrderByDescending(x => x.Price);
break;
case ProductSortingEnum.CreatedOn:
//creation date
- query = _catalogSettings.SortingByAvailability ?
- query.OrderBy(x => x.LowStock).ThenBy(x => x.CreatedOnUtc) :
- query.OrderBy(x => x.CreatedOnUtc);
+ query = _catalogSettings.SortingByAvailability
+ ? query.OrderBy(x => x.LowStock).ThenBy(x => x.CreatedOnUtc)
+ : query.OrderBy(x => x.CreatedOnUtc);
break;
case ProductSortingEnum.OnSale:
//on sale
query = query.OrderBy(x => x.OnSale);
- query = _catalogSettings.SortingByAvailability ?
- query.OrderBy(x => x.LowStock).ThenBy(x => x.OnSale) :
- query.OrderBy(x => x.OnSale);
+ query = _catalogSettings.SortingByAvailability
+ ? query.OrderBy(x => x.LowStock).ThenBy(x => x.OnSale)
+ : query.OrderBy(x => x.OnSale);
break;
case ProductSortingEnum.MostViewed:
//most viewed
- query = _catalogSettings.SortingByAvailability ?
- query.OrderBy(x => x.LowStock).ThenByDescending(x => x.Viewed) :
- query.OrderByDescending(x => x.Viewed);
+ query = _catalogSettings.SortingByAvailability
+ ? query.OrderBy(x => x.LowStock).ThenByDescending(x => x.Viewed)
+ : query.OrderByDescending(x => x.Viewed);
break;
case ProductSortingEnum.BestSellers:
//best seller
query = query.OrderByDescending(x => x.Sold);
- query = _catalogSettings.SortingByAvailability ?
- query.OrderBy(x => x.LowStock).ThenByDescending(x => x.Sold) :
- query.OrderByDescending(x => x.Sold);
+ query = _catalogSettings.SortingByAvailability
+ ? query.OrderBy(x => x.LowStock).ThenByDescending(x => x.Sold)
+ : query.OrderByDescending(x => x.Sold);
+ break;
+ case ProductSortingEnum.Rating:
+ //most viewed
+ query = _catalogSettings.SortingByAvailability
+ ? query.OrderBy(x => x.LowStock).ThenByDescending(x => x.AvgRating)
+ : query.OrderByDescending(x => x.AvgRating).ThenByDescending(x => x.ApprovedTotalReviews);
break;
default:
break;
@@ -320,14 +343,17 @@ namespace Grand.Business.Catalog.Queries.Handlers
_catalogSettings.IgnoreFilterableSpecAttributeOption)
return (products, filterableSpecificationAttributeOptionIds);
{
- 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();
- IList<string> specyfication = groupQuerySpec.Select(item => item.Key.SpecificationAttributeOptionId).ToList();
+ var groupQuerySpec = qspec.Where(x => x.AllowFiltering).GroupBy(x =>
+ new { SpecificationAttributeOptionId = x.SpecificationAttributeOptionId }).ToList();
+ IList<string> specyfication =
+ groupQuerySpec.Select(item => item.Key.SpecificationAttributeOptionId).ToList();
filterableSpecificationAttributeOptionIds = specyfication.ToList();
}
@@ -337,4 +363,4 @@ namespace Grand.Business.Catalog.Queries.Handlers
#endregion
}
}
-}
+}
\ No newline at end of file
diff --git a/src/Business/Grand.Business.System/Services/Installation/InstallDataProducts.cs b/src/Business/Grand.Business.System/Services/Installation/InstallDataProducts.cs
index cf32710fe..48bb64b28 100644
--- a/src/Business/Grand.Business.System/Services/Installation/InstallDataProducts.cs
+++ b/src/Business/Grand.Business.System/Services/Installation/InstallDataProducts.cs
@@ -3750,8 +3750,8 @@ namespace Grand.Business.System.Services.Installation
await _productReviewRepository.InsertAsync(productReview);
product.ApprovedRatingSum = rating;
- product.ApprovedTotalReviews = product.ApprovedTotalReviews + 1;
-
+ product.ApprovedTotalReviews += 1;
+ product.AvgRating = rating / product.ApprovedTotalReviews;
}
await _productRepository.UpdateAsync(allProducts);
}
diff --git a/src/Business/Grand.Business.System/Services/Migrations/2.1/MigrationProductRating.cs b/src/Business/Grand.Business.System/Services/Migrations/2.1/MigrationProductRating.cs
new file mode 100644
index 000000000..25897c987
--- /dev/null
+++ b/src/Business/Grand.Business.System/Services/Migrations/2.1/MigrationProductRating.cs
@@ -0,0 +1,46 @@
+using Grand.Business.Core.Interfaces.Common.Logging;
+using Grand.Domain.Catalog;
+using Grand.Domain.Data;
+using Grand.Domain.Logging;
+using Grand.Infrastructure.Migrations;
+using Microsoft.Extensions.DependencyInjection;
+
+namespace Grand.Business.System.Services.Migrations._2._1
+{
+ public class MigrationProductRating : IMigration
+ {
+
+ public int Priority => 0;
+ public DbVersion Version => new(2, 1);
+ public Guid Identity => new("4AD1CA78-5FE4-4A6C-A70C-D2036FA32E3E");
+ public string Name => "Update average product rating";
+
+ /// <summary>
+ /// Upgrade process
+ /// </summary>
+ /// <param name="database"></param>
+ /// <param name="serviceProvider"></param>
+ /// <returns></returns>
+ public bool UpgradeProcess(IDatabaseContext database, IServiceProvider serviceProvider)
+ {
+ var productRepository = serviceProvider.GetRequiredService<IRepository<Product>>();
+ var logService = serviceProvider.GetRequiredService<ILogger>();
+ try
+ {
+ foreach (var product in productRepository.Table)
+ {
+ var update = UpdateBuilder<Product>.Create()
+ .Set(x => x.AvgRating, product.ApprovedTotalReviews == 0 ? 0 : Math.Round((double)product.ApprovedRatingSum / product.ApprovedTotalReviews, 2));
+
+ productRepository.UpdateOneAsync(x=>x.Id == product.Id, update).GetAwaiter().GetResult();
+ }
+ }
+ catch (Exception ex)
+ {
+ logService.InsertLog(LogLevel.Error, "UpgradeProcess - RemoveOldActivityLogType", ex.Message).GetAwaiter().GetResult();
+ }
+ return true;
+
+ }
+ }
+}
diff --git a/src/Core/Grand.Domain/Catalog/Product.cs b/src/Core/Grand.Domain/Catalog/Product.cs
index 07d321bf0..9491b50c0 100644
--- a/src/Core/Grand.Domain/Catalog/Product.cs
+++ b/src/Core/Grand.Domain/Catalog/Product.cs
@@ -127,6 +127,10 @@ namespace Grand.Domain.Catalog
/// </summary>
public int ApprovedTotalReviews { get; set; }
/// <summary>
+ /// Gets or sets the average rating (approved reviews)
+ /// </summary>
+ public double AvgRating { get; set; }
+ /// <summary>
/// Gets or sets the total rating votes (not approved reviews)
/// </summary>
public int NotApprovedTotalReviews { get; set; }
diff --git a/src/Core/Grand.Domain/Catalog/ProductSortingEnum.cs b/src/Core/Grand.Domain/Catalog/ProductSortingEnum.cs
index 6f9fb960b..732fc4748 100644
--- a/src/Core/Grand.Domain/Catalog/ProductSortingEnum.cs
+++ b/src/Core/Grand.Domain/Catalog/ProductSortingEnum.cs
@@ -38,9 +38,12 @@
/// </summary>
MostViewed = 25,
/// <summary>
- /// Product most onsale
+ /// Product most on-sale
/// </summary>
OnSale = 30,
-
+ /// <summary>
+ /// Product rating
+ /// </summary>
+ Rating = 40
}
}
\ 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 18ecb9921..72e0db8d7 100644
--- a/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml
+++ b/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml
@@ -13238,6 +13238,9 @@
<Resource Name="Enums.Grand.Domain.Catalog.ProductSortingEnum.PriceDesc" Area="Common">
<Value>Price: High to Low</Value>
</Resource>
+ <Resource Name="Enums.Grand.Domain.Catalog.ProductSortingEnum.Rating" Area="Common">
+ <Value>Rating</Value>
+ </Resource>
<Resource Name="Enums.Grand.Domain.Catalog.ProductType.BundledProduct" Area="Common">
<Value>Bundled product</Value>
</Resource>
diff --git a/src/Web/Grand.Web/App_Data/Resources/Upgrade/en_210.xml b/src/Web/Grand.Web/App_Data/Resources/Upgrade/en_210.xml
index 3efc9b524..2b2f588e0 100644
--- a/src/Web/Grand.Web/App_Data/Resources/Upgrade/en_210.xml
+++ b/src/Web/Grand.Web/App_Data/Resources/Upgrade/en_210.xml
@@ -75,4 +75,7 @@
<Resource Name="OutOfStockSubscriptions.OnlyRegisteredText" Area="Front">
<Value>Only registered customers can use this feature</Value>
</Resource>
+ <Resource Name="Enums.Grand.Domain.Catalog.ProductSortingEnum.Rating" Area="Common">
+ <Value>Rating</Value>
+ </Resource>
</Language>
\ No newline at end of file
diff --git a/src/Web/Grand.Web/Features/Handlers/Catalog/GetSearchAutoCompleteHandler.cs b/src/Web/Grand.Web/Features/Handlers/Catalog/GetSearchAutoCompleteHandler.cs
index 0b9cf7471..91e61f946 100644
--- a/src/Web/Grand.Web/Features/Handlers/Catalog/GetSearchAutoCompleteHandler.cs
+++ b/src/Web/Grand.Web/Features/Handlers/Catalog/GetSearchAutoCompleteHandler.cs
@@ -133,7 +133,7 @@ namespace Grand.Web.Features.Handlers.Catalog
Desc = item.GetTranslation(x => x.ShortDescription, request.Language.Id) ?? "",
PictureUrl = pictureUrl,
AllowCustomerReviews = rating.AllowCustomerReviews,
- Rating = rating.TotalReviews > 0 ? rating.RatingSum * 100 / rating.TotalReviews / 5 : 0,
+ Rating = rating.AvgRating,
Price = price.Price,
PriceWithDiscount = price.PriceWithDiscount,
Url = $"{storeurl}/{item.SeName}"
diff --git a/src/Web/Grand.Web/Features/Handlers/Products/GetProductReviewOverviewHandler.cs b/src/Web/Grand.Web/Features/Handlers/Products/GetProductReviewOverviewHandler.cs
index 7655cbd26..5b2ed3007 100644
--- a/src/Web/Grand.Web/Features/Handlers/Products/GetProductReviewOverviewHandler.cs
+++ b/src/Web/Grand.Web/Features/Handlers/Products/GetProductReviewOverviewHandler.cs
@@ -11,6 +11,7 @@ namespace Grand.Web.Features.Handlers.Products
var productReview = new ProductReviewOverviewModel {
RatingSum = request.Product.ApprovedRatingSum,
TotalReviews = request.Product.ApprovedTotalReviews,
+ AvgRating = request.Product.AvgRating,
ProductId = request.Product.Id,
AllowCustomerReviews = request.Product.AllowCustomerReviews
};
diff --git a/src/Web/Grand.Web/Models/Catalog/ProductReviewModel.cs b/src/Web/Grand.Web/Models/Catalog/ProductReviewModel.cs
index 6f23bbab3..fe6d20011 100644
--- a/src/Web/Grand.Web/Models/Catalog/ProductReviewModel.cs
+++ b/src/Web/Grand.Web/Models/Catalog/ProductReviewModel.cs
@@ -10,6 +10,7 @@ namespace Grand.Web.Models.Catalog
public int RatingSum { get; set; }
public int TotalReviews { get; set; }
+ public double AvgRating { get; set; }
public bool AllowCustomerReviews { get; set; }
}
diff --git a/src/Web/Grand.Web/Models/Catalog/SearchAutoCompleteModel.cs b/src/Web/Grand.Web/Models/Catalog/SearchAutoCompleteModel.cs
index 685274b6b..5aea2d5d7 100644
--- a/src/Web/Grand.Web/Models/Catalog/SearchAutoCompleteModel.cs
+++ b/src/Web/Grand.Web/Models/Catalog/SearchAutoCompleteModel.cs
@@ -9,7 +9,7 @@
public string Price { get; set; }
public string PriceWithDiscount { get; set; }
public bool AllowCustomerReviews { get; set; }
- public int Rating { get; set; }
+ public double Rating { get; set; }
public string PictureUrl { get; set; }
}
}
diff --git a/src/Web/Grand.Web/Views/Product/Partials/ProductReviewOverview.cshtml b/src/Web/Grand.Web/Views/Product/Partials/ProductReviewOverview.cshtml
index b076c9ab3..2dd3bb432 100644
--- a/src/Web/Grand.Web/Views/Product/Partials/ProductReviewOverview.cshtml
+++ b/src/Web/Grand.Web/Views/Product/Partials/ProductReviewOverview.cshtml
@@ -1,17 +1,11 @@
@using System.Globalization
@model ProductReviewOverviewModel
-@{
- var ratingPercent = 0;
- if (Model.TotalReviews != 0)
- {
- ratingPercent = Model.RatingSum * 100 / Model.TotalReviews / 5;
- }
-}
+
<div class="product-reviews-overview d-inline-flex align-items-center flex-wrap" @if (Model.TotalReviews > 0)
{ <text> itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating" </text> }>
<div class="product-review-box d-inline-block order-1">
<div class="rating">
- <b-form-rating id="[email protected]" class="p-0" variant="warning" no-border size="sm" show-value precision="2" readonly inline v-model="productreviewsoverview.rating"></b-form-rating>
+ <b-form-rating id="[email protected]" class="p-0" variant="warning" no-border size="sm" show-value precision="2" readonly inline v-model="productreviewsoverview.Model.AvgRating"></b-form-rating>
<template v-if="productreviewsoverview.Model.TotalReviews > 0">
<div class="product-review-links order-3">
<b-button variant="outline-info" size="sm" class="review-scroll-button" @@click="active = true" href="#review-tab___BV_tab_button__">
@@ -22,15 +16,14 @@
</template>
<template v-else>
<div class="product-no-reviews order-3">
- <a class="btn btn-sm btn-outline-info" @@click="productreviews.addProductReview()">
+ <a class="btn btn-sm btn-outline-info" @@click="productreviews.addProductReview()">
@Loc["Reviews.Overview.First"]
</a>
</div>
</template>
@{
@* rendering microdata info*@
- var value = ratingPercent / (double)20;
- <span itemprop="ratingValue" class="d-none">(@value.ToString("0.0", new CultureInfo("en-US")))</span>
+ <span itemprop="ratingValue" class="d-none">@Model.AvgRating.ToString("0.0", new CultureInfo("en-US"))</span>
<span itemprop="reviewCount" class="d-none">@Model.TotalReviews</span>
}
</div>
@@ -41,18 +34,10 @@
data: function () {
return {
Model: null,
- rating: 0
}
},
created() {
this.Model = @Json.Serialize(Model);
- },
- watch: {
- Model: function() {
- const RatingSum = this.Model.RatingSum;
- const TotalReviews = this.Model.TotalReviews;
- this.rating = RatingSum / TotalReviews;
- }
- }
+ },
})
</script>
diff --git a/src/Web/Grand.Web/Views/Shared/Components/SearchBox/Default.cshtml b/src/Web/Grand.Web/Views/Shared/Components/SearchBox/Default.cshtml
index deaac8c8d..0a67eed5d 100644
--- a/src/Web/Grand.Web/Views/Shared/Components/SearchBox/Default.cshtml
+++ b/src/Web/Grand.Web/Views/Shared/Components/SearchBox/Default.cshtml
@@ -103,7 +103,7 @@
</div>
<div class="media-body">
<h6 class="mb-0"><a :href="item.Url">{{item.Label}}</a></h6>
- <b-form-rating class="p-0" variant="warning" no-border size="sm" show-value precision="2" readonly inline :value="item.Rating / 20"></b-form-rating>
+ <b-form-rating class="p-0" variant="warning" no-border size="sm" show-value precision="2" readonly inline :value="item.Rating"></b-form-rating>
<div v-if="item.Price" class="price">{{item.Price}}</div>
<p v-html="item.Desc" class="mt-1 mb-0"></p>
</div>
diff --git a/src/Web/Grand.Web/Views/Shared/Partials/CatalogProductView.cshtml b/src/Web/Grand.Web/Views/Shared/Partials/CatalogProductView.cshtml
index e866a3a42..fbe56aedc 100644
--- a/src/Web/Grand.Web/Views/Shared/Partials/CatalogProductView.cshtml
+++ b/src/Web/Grand.Web/Views/Shared/Partials/CatalogProductView.cshtml
@@ -54,14 +54,9 @@
<div class="prices-rating">
@if (Model.ReviewOverviewModel.AllowCustomerReviews)
{
- double ratingValue = 0;
- if (Model.ReviewOverviewModel.TotalReviews != 0)
- {
- ratingValue = Convert.ToDouble(Model.ReviewOverviewModel.RatingSum * (double)100 / Model.ReviewOverviewModel.TotalReviews / 100);
- }
<template>
<div class="rating">
- <b-form-rating id="[email protected]" aria-label="Rating" class="p-0" variant="warning" no-border size="sm" show-value aria-valuenow="@(ratingValue.ToString("0.00", CultureInfo.InvariantCulture))" precision="2" readonly inline value="@(ratingValue.ToString("0.00", CultureInfo.InvariantCulture))"></b-form-rating>
+ <b-form-rating id="[email protected]" aria-label="Rating" class="p-0" variant="warning" no-border size="sm" show-value aria-valuenow="@(Model.ReviewOverviewModel.AvgRating.ToString("0.00", CultureInfo.InvariantCulture))" precision="2" readonly inline value="@Model.ReviewOverviewModel.AvgRating.ToString("0.00", CultureInfo.InvariantCulture)"></b-form-rating>
<b-link href="@Url.RouteUrl("Product", new { Model.SeName })">@Model.ReviewOverviewModel.TotalReviews @Loc["Reviews.Overview.Reviews"]</b-link>
</div>
</template>