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.

Allow manging bestsellers manually directly on the product #5

KrzysztofPajak82d5f3f323f7
diff --git a/src/Business/Grand.Business.Catalog/Interfaces/Products/IProductService.cs b/src/Business/Grand.Business.Catalog/Interfaces/Products/IProductService.cs
index b1df7a116..f252497d4 100644
--- a/src/Business/Grand.Business.Catalog/Interfaces/Products/IProductService.cs
+++ b/src/Business/Grand.Business.Catalog/Interfaces/Products/IProductService.cs
@@ -20,6 +20,12 @@ namespace Grand.Business.Catalog.Interfaces.Products
/// <returns>Products</returns>
Task<IList<string>> GetAllProductsDisplayedOnHomePage();
+ /// <summary>
+ /// Gets all products displayed on the best seller
+ /// </summary>
+ /// <returns>Products</returns>
+ Task<IList<string>> GetAllProductsDisplayedOnBestSeller();
+
/// <summary>
/// Gets product
/// </summary>
diff --git a/src/Business/Grand.Business.Catalog/Services/Products/CopyProductService.cs b/src/Business/Grand.Business.Catalog/Services/Products/CopyProductService.cs
index f53994190..b4b92a9f3 100644
--- a/src/Business/Grand.Business.Catalog/Services/Products/CopyProductService.cs
+++ b/src/Business/Grand.Business.Catalog/Services/Products/CopyProductService.cs
@@ -127,6 +127,7 @@ namespace Grand.Business.Catalog.Services.Products
ProductLayoutId = product.ProductLayoutId,
AdminComment = product.AdminComment,
ShowOnHomePage = product.ShowOnHomePage,
+ BestSeller = product.BestSeller,
MetaKeywords = product.MetaKeywords,
MetaDescription = product.MetaDescription,
MetaTitle = product.MetaTitle,
diff --git a/src/Business/Grand.Business.Catalog/Services/Products/ProductService.cs b/src/Business/Grand.Business.Catalog/Services/Products/ProductService.cs
index 35603a262..ac73d2a1d 100644
--- a/src/Business/Grand.Business.Catalog/Services/Products/ProductService.cs
+++ b/src/Business/Grand.Business.Catalog/Services/Products/ProductService.cs
@@ -82,6 +82,25 @@ namespace Grand.Business.Catalog.Services.Products
return products;
}
+ /// <summary>
+ /// Gets all products displayed on the best seller
+ /// </summary>
+ /// <returns>Products</returns>
+ public virtual async Task<IList<string>> GetAllProductsDisplayedOnBestSeller()
+ {
+ var builder = Builders<Product>.Filter;
+ var filter = builder.Eq(x => x.Published, true);
+ filter &= builder.Eq(x => x.BestSeller, true);
+ filter &= builder.Eq(x => x.VisibleIndividually, true);
+ var query = _productRepository.Collection.Find(filter).
+ SortBy(x => x.DisplayOrder).ThenBy(x => x.Name).
+ Project(x => x.Id);
+
+ var products = await query.ToListAsync();
+
+ return products;
+ }
+
/// <summary>
/// Gets product
/// </summary>
@@ -278,6 +297,7 @@ namespace Grand.Business.Catalog.Services.Products
.Set(x => x.ShipSeparately, product.ShipSeparately)
.Set(x => x.ShortDescription, product.ShortDescription)
.Set(x => x.ShowOnHomePage, product.ShowOnHomePage)
+ .Set(x => x.BestSeller, product.BestSeller)
.Set(x => x.Sku, product.Sku)
.Set(x => x.StartPrice, product.StartPrice)
.Set(x => x.StockQuantity, product.StockQuantity)
diff --git a/src/Business/Grand.Business.System/Services/ExportImport/ExportManager.cs b/src/Business/Grand.Business.System/Services/ExportImport/ExportManager.cs
index 410fd53a7..b3d72eefc 100644
--- a/src/Business/Grand.Business.System/Services/ExportImport/ExportManager.cs
+++ b/src/Business/Grand.Business.System/Services/ExportImport/ExportManager.cs
@@ -162,6 +162,7 @@ namespace Grand.Business.System.Services.ExportImport
new PropertyByName<Product>("VendorId", p => p.VendorId),
new PropertyByName<Product>("ProductLayoutId", p => p.ProductLayoutId),
new PropertyByName<Product>("ShowOnHomePage", p => p.ShowOnHomePage),
+ new PropertyByName<Product>("BestSeller", p => p.BestSeller),
new PropertyByName<Product>("MetaKeywords", p => p.MetaKeywords),
new PropertyByName<Product>("MetaDescription", p => p.MetaDescription),
new PropertyByName<Product>("MetaTitle", p => p.MetaTitle),
diff --git a/src/Business/Grand.Business.System/Services/ExportImport/ImportManager.cs b/src/Business/Grand.Business.System/Services/ExportImport/ImportManager.cs
index b6d93c335..30f905ee9 100644
--- a/src/Business/Grand.Business.System/Services/ExportImport/ImportManager.cs
+++ b/src/Business/Grand.Business.System/Services/ExportImport/ImportManager.cs
@@ -197,6 +197,9 @@ namespace Grand.Business.System.Services.ExportImport
case "showonhomepage":
product.ShowOnHomePage = property.BooleanValue;
break;
+ case "bestseller":
+ product.BestSeller = property.BooleanValue;
+ break;
case "metakeywords":
product.MetaKeywords = property.StringValue;
break;
diff --git a/src/Business/Grand.Business.System/Services/Installation/InstallDataSettings.cs b/src/Business/Grand.Business.System/Services/Installation/InstallDataSettings.cs
index be12ff62d..28fe7f2a4 100644
--- a/src/Business/Grand.Business.System/Services/Installation/InstallDataSettings.cs
+++ b/src/Business/Grand.Business.System/Services/Installation/InstallDataSettings.cs
@@ -221,6 +221,7 @@ namespace Grand.Business.System.Services.Installation
ShowProductImagesInSearchAutoComplete = true,
ShowBestsellersOnHomepage = false,
NumberOfBestsellersOnHomepage = 4,
+ BestsellersFromReports = false,
PeriodBestsellers = 6,
NumberOfReview = 10,
SearchPageProductsPerPage = 6,
diff --git a/src/Business/Grand.Business.System/Services/Installation/InstallationService.cs b/src/Business/Grand.Business.System/Services/Installation/InstallationService.cs
index 91e5b7ae3..33a3dda0d 100644
--- a/src/Business/Grand.Business.System/Services/Installation/InstallationService.cs
+++ b/src/Business/Grand.Business.System/Services/Installation/InstallationService.cs
@@ -318,6 +318,7 @@ namespace Grand.Business.System.Services.Installation
//Product
await _productRepository.Collection.Indexes.CreateOneAsync(new CreateIndexModel<Product>((Builders<Product>.IndexKeys.Ascending(x => x.MarkAsNew).Ascending(x => x.CreatedOnUtc)), new CreateIndexOptions() { Name = "MarkAsNew_1_CreatedOnUtc_1", Unique = false }));
await _productRepository.Collection.Indexes.CreateOneAsync(new CreateIndexModel<Product>((Builders<Product>.IndexKeys.Ascending(x => x.Published).Ascending(x => x.ShowOnHomePage).Ascending(x => x.DisplayOrder).Ascending(x => x.Name)), new CreateIndexOptions() { Name = "ShowOnHomePage_1_Published_1", Unique = false }));
+ await _productRepository.Collection.Indexes.CreateOneAsync(new CreateIndexModel<Product>((Builders<Product>.IndexKeys.Ascending(x => x.Published).Ascending(x => x.BestSeller).Ascending(x => x.DisplayOrder).Ascending(x => x.Name)), new CreateIndexOptions() { Name = "ShowOnBestSeller_1_Published_1", Unique = false }));
await _productRepository.Collection.Indexes.CreateOneAsync(new CreateIndexModel<Product>((Builders<Product>.IndexKeys.Ascending(x => x.ParentGroupedProductId).Ascending(x => x.DisplayOrder)), new CreateIndexOptions() { Name = "ParentGroupedProductId_1_DisplayOrder_1", Unique = false }));
await _productRepository.Collection.Indexes.CreateOneAsync(new CreateIndexModel<Product>((Builders<Product>.IndexKeys.Ascending(x => x.ProductTags).Ascending(x => x.Published).Ascending(x => x.VisibleIndividually).Ascending(x => x.Name)), new CreateIndexOptions() { Name = "ProductTags._id_1_Name_1", Unique = false }));
await _productRepository.Collection.Indexes.CreateOneAsync(new CreateIndexModel<Product>((Builders<Product>.IndexKeys.Ascending(x => x.Name)), new CreateIndexOptions() { Name = "Name_1", Unique = false }));
diff --git a/src/Core/Grand.Domain/Catalog/CatalogSettings.cs b/src/Core/Grand.Domain/Catalog/CatalogSettings.cs
index c23da4f0d..e52db7e05 100644
--- a/src/Core/Grand.Domain/Catalog/CatalogSettings.cs
+++ b/src/Core/Grand.Domain/Catalog/CatalogSettings.cs
@@ -266,6 +266,11 @@ namespace Grand.Domain.Catalog
/// </summary>
public int NumberOfBestsellersOnHomepage { get; set; }
+ /// <summary>
+ /// Gets or sets a value indicating whether to use bestsellers from reports
+ /// </summary>
+ public bool BestsellersFromReports { get; set; }
+
/// <summary>
/// Gets or sets a number of time period for bestsellers on home page
/// </summary>
diff --git a/src/Core/Grand.Domain/Catalog/Product.cs b/src/Core/Grand.Domain/Catalog/Product.cs
index d2769c8de..b88f4380a 100644
--- a/src/Core/Grand.Domain/Catalog/Product.cs
+++ b/src/Core/Grand.Domain/Catalog/Product.cs
@@ -93,6 +93,11 @@ namespace Grand.Domain.Catalog
/// </summary>
public bool ShowOnHomePage { get; set; }
+ /// <summary>
+ /// Gets or sets a value indicating whether to show the product on the bestseller section
+ /// </summary>
+ public bool BestSeller { get; set; }
+
/// <summary>
/// Gets or sets the meta keywords
/// </summary>
diff --git a/src/Web/Grand.Web.Admin/Areas/Admin/Views/Product/_CreateOrUpdate.Info.cshtml b/src/Web/Grand.Web.Admin/Areas/Admin/Views/Product/_CreateOrUpdate.Info.cshtml
index 27833d72e..9c39d011b 100644
--- a/src/Web/Grand.Web.Admin/Areas/Admin/Views/Product/_CreateOrUpdate.Info.cshtml
+++ b/src/Web/Grand.Web.Admin/Areas/Admin/Views/Product/_CreateOrUpdate.Info.cshtml
@@ -17,6 +17,7 @@
@if (!Model.IsLoggedInAsVendor)
{
<text>$("#@Html.IdFor(model => model.ShowOnHomePage)").click(toggleShowOnHomePage);</text>
+ <text>$("#@Html.IdFor(model => model.BestSeller)").click(toggleShowOnHomePage);</text>
}
$("#@Html.IdFor(model => model.ProductTypeId)").change(toggleProductType);
$("#@Html.IdFor(model => model.RequireOtherProducts)").click(toggleRequireOtherProducts);
@@ -61,6 +62,9 @@
} else {
$('#pnlDisplayOrder').hide();
}
+ if ($('#@Html.IdFor(model => model.BestSeller)').is(':checked')) {
+ $('#pnlDisplayOrder').show();
+ }
}
function toggleProductType() {
@@ -626,6 +630,16 @@
<span asp-validation-for="DisplayOrder"></span>
</div>
</div>
+ <div class="form-group" @(Model.IsLoggedInAsVendor ? Html.Raw("style='display: none;'") : null)>
+ <admin-label asp-for="BestSeller" />
+ <div class="col-md-9 col-sm-9">
+ <label class="mt-checkbox mt-checkbox-outline control control-checkbox">
+ <admin-input asp-for="BestSeller" />
+ <div class="control__indicator"></div>
+ </label>
+ <span asp-validation-for="BestSeller"></span>
+ </div>
+ </div>
<div class="form-group">
<admin-label asp-for="AllowCustomerReviews" />
<div class="col-md-9 col-sm-9">
diff --git a/src/Web/Grand.Web.Admin/Areas/Admin/Views/Setting/Catalog.TabGeneralSettings.cshtml b/src/Web/Grand.Web.Admin/Areas/Admin/Views/Setting/Catalog.TabGeneralSettings.cshtml
index 68c246180..05a5d7d1f 100644
--- a/src/Web/Grand.Web.Admin/Areas/Admin/Views/Setting/Catalog.TabGeneralSettings.cshtml
+++ b/src/Web/Grand.Web.Admin/Areas/Admin/Views/Setting/Catalog.TabGeneralSettings.cshtml
@@ -473,6 +473,18 @@
<span asp-validation-for="NumberOfBestsellersOnHomepage"></span>
</div>
</div>
+ <div class="form-group" id="pnlNumberOfBestsellersOnHomepage">
+ <div class="col-6 col-md-6 col-sm-6">
+ <admin-label asp-for="BestsellersFromReports" class="control-label" />
+ </div>
+ <div class="col-6 col-md-6 col-sm-6">
+ <label class="mt-checkbox mt-checkbox-outline control control-checkbox">
+ <admin-input asp-for="BestsellersFromReports" />
+ <div class="control__indicator"></div>
+ </label>
+ <span asp-validation-for="BestsellersFromReports"></span>
+ </div>
+ </div>
<div class="form-group">
<div class="col-6 col-md-6 col-sm-6">
<admin-label asp-for="ProductsAlsoPurchasedEnabled" class="control-label" />
diff --git a/src/Web/Grand.Web.Admin/Models/Catalog/ProductModel.cs b/src/Web/Grand.Web.Admin/Models/Catalog/ProductModel.cs
index 0d49f451d..64ec0e93a 100644
--- a/src/Web/Grand.Web.Admin/Models/Catalog/ProductModel.cs
+++ b/src/Web/Grand.Web.Admin/Models/Catalog/ProductModel.cs
@@ -83,6 +83,9 @@ namespace Grand.Web.Admin.Models.Catalog
[GrandResourceDisplayName("Admin.Catalog.Products.Fields.ShowOnHomePage")]
public bool ShowOnHomePage { get; set; }
+ [GrandResourceDisplayName("Admin.Catalog.Products.Fields.BestSeller")]
+ public bool BestSeller { get; set; }
+
[GrandResourceDisplayName("Admin.Catalog.Products.Fields.MetaKeywords")]
public string MetaKeywords { get; set; }
diff --git a/src/Web/Grand.Web.Admin/Models/Settings/CatalogSettingsModel.cs b/src/Web/Grand.Web.Admin/Models/Settings/CatalogSettingsModel.cs
index 0adae31a6..8f3d8cd34 100644
--- a/src/Web/Grand.Web.Admin/Models/Settings/CatalogSettingsModel.cs
+++ b/src/Web/Grand.Web.Admin/Models/Settings/CatalogSettingsModel.cs
@@ -119,6 +119,9 @@ namespace Grand.Web.Admin.Models.Settings
[GrandResourceDisplayName("Admin.Settings.Catalog.NumberOfBestsellersOnHomepage")]
public int NumberOfBestsellersOnHomepage { get; set; }
+ [GrandResourceDisplayName("Admin.Settings.Catalog.BestsellersFromReports")]
+ public bool BestsellersFromReports { get; set; }
+
[GrandResourceDisplayName("Admin.Settings.Catalog.SearchPageProductsPerPage")]
public int SearchPageProductsPerPage { get; set; }
diff --git a/src/Web/Grand.Web.Admin/Services/ProductViewModelService.cs b/src/Web/Grand.Web.Admin/Services/ProductViewModelService.cs
index 1090c056d..3481967f7 100644
--- a/src/Web/Grand.Web.Admin/Services/ProductViewModelService.cs
+++ b/src/Web/Grand.Web.Admin/Services/ProductViewModelService.cs
@@ -894,9 +894,10 @@ namespace Grand.Web.Admin.Services
}
//vendors cannot edit "Show on home page" property
- if (_workContext.CurrentVendor != null && model.ShowOnHomePage)
+ if (_workContext.CurrentVendor != null && (model.ShowOnHomePage || model.BestSeller))
{
model.ShowOnHomePage = false;
+ model.BestSeller = false;
}
//product
@@ -949,6 +950,11 @@ namespace Grand.Web.Admin.Services
{
model.ShowOnHomePage = product.ShowOnHomePage;
}
+ //vendors cannot edit "Best seller" property
+ if (_workContext.CurrentVendor != null && model.BestSeller != product.BestSeller)
+ {
+ model.BestSeller = product.BestSeller;
+ }
//a staff should have access only to his products
if (await _groupService.IsStaff(_workContext.CurrentCustomer))
diff --git a/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml b/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml
index 7ce96553a..c500fadfc 100644
--- a/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml
+++ b/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml
@@ -2829,6 +2829,9 @@
<Resource Name="Admin.Catalog.Products.Fields.BasepriceUnit">
<Value>Unit of the product being sold</Value>
</Resource>
+ <Resource Name="Admin.Catalog.Products.Fields.BestSeller">
+ <Value>Best seller</Value>
+ </Resource>
<Resource Name="Admin.Catalog.Products.Fields.Brand">
<Value>Brand</Value>
</Resource>
@@ -10474,6 +10477,9 @@
<Resource Name="Admin.Settings.Catalog.AskQuestionOnProduct">
<Value>'Ask question on the product page' enabled</Value>
</Resource>
+ <Resource Name="Admin.Settings.Catalog.BestsellersFromReports">
+ <Value>Best seller products get from report</Value>
+ </Resource>
<Resource Name="Admin.Settings.Catalog.CategoryBreadcrumbEnabled">
<Value>Category breadcrumb enabled</Value>
</Resource>
diff --git a/src/Web/Grand.Web/Components/HomePageBestSellers.cs b/src/Web/Grand.Web/Components/HomePageBestSellers.cs
index 8e6b2a196..a38604c37 100644
--- a/src/Web/Grand.Web/Components/HomePageBestSellers.cs
+++ b/src/Web/Grand.Web/Components/HomePageBestSellers.cs
@@ -11,6 +11,7 @@ using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using System.Threading.Tasks;
+using System.Collections.Generic;
namespace Grand.Web.Components
{
@@ -54,17 +55,29 @@ namespace Grand.Web.Components
if (!_catalogSettings.ShowBestsellersOnHomepage || _catalogSettings.NumberOfBestsellersOnHomepage == 0)
return Content("");
+ var productIds = new List<string>();
+
//load and cache report
- var fromdate = DateTime.UtcNow.AddMonths(_catalogSettings.PeriodBestsellers > 0 ? -_catalogSettings.PeriodBestsellers : -12);
- var report = await _cacheBase.GetAsync(string.Format(CacheKeyConst.HOMEPAGE_BESTSELLERS_IDS_KEY, _workContext.CurrentStore.Id), async () =>
- await _orderReportService.BestSellersReport(
- createdFromUtc: fromdate,
- ps: Domain.Payments.PaymentStatus.Paid,
- storeId: _workContext.CurrentStore.Id,
- pageSize: _catalogSettings.NumberOfBestsellersOnHomepage));
+ if (_catalogSettings.BestsellersFromReports)
+ {
+ var fromdate = DateTime.UtcNow.AddMonths(_catalogSettings.PeriodBestsellers > 0 ? -_catalogSettings.PeriodBestsellers : -12);
+ var report = await _cacheBase.GetAsync(string.Format(CacheKeyConst.HOMEPAGE_BESTSELLERS_IDS_KEY, _workContext.CurrentStore.Id), async () =>
+ await _orderReportService.BestSellersReport(
+ createdFromUtc: fromdate,
+ ps: Domain.Payments.PaymentStatus.Paid,
+ storeId: _workContext.CurrentStore.Id,
+ pageSize: _catalogSettings.NumberOfBestsellersOnHomepage));
+
+ productIds = report.Select(x => x.ProductId).ToList();
+ }
+ else
+ {
+ productIds = await _cacheBase.GetAsync(CacheKeyConst.BESTSELLER_PRODUCTS_MODEL_KEY,
+ async () => (await _productService.GetAllProductsDisplayedOnBestSeller()).ToList());
+ }
//load products
- var products = await _productService.GetProductsByIds(report.Select(x => x.ProductId).ToArray());
+ var products = await _productService.GetProductsByIds(productIds.ToArray());
if (!products.Any())
return Content("");
diff --git a/src/Web/Grand.Web/Events/Cache/CacheKeyConst.cs b/src/Web/Grand.Web/Events/Cache/CacheKeyConst.cs
index e3a1ada19..cdeb1998a 100644
--- a/src/Web/Grand.Web/Events/Cache/CacheKeyConst.cs
+++ b/src/Web/Grand.Web/Events/Cache/CacheKeyConst.cs
@@ -254,6 +254,13 @@ namespace Grand.Web.Events.Cache
/// </remarks>
public const string HOMEPAGE_PRODUCTS_MODEL_KEY = "Grand.product.homepage";
+ /// <summary>
+ /// Key for Show best seller product model caching
+ /// </summary>
+ /// <remarks>
+ /// </remarks>
+ public const string BESTSELLER_PRODUCTS_MODEL_KEY = "Grand.product.homepage.bestseller";
+
/// <summary>
/// Key for "also purchased" product identifiers displayed on the product details page
/// </summary>