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 component RecommendedProducts

KrzysztofPajakebea607f7215
diff --git a/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml b/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml
index c8212afa3..b36232d8a 100644
--- a/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml
+++ b/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml
@@ -15423,6 +15423,9 @@
<Resource Name="Products.RecentlyViewedProducts">
<Value>Recently viewed products</Value>
</Resource>
+ <Resource Name="Products.RecommendedProducts">
+ <Value>Recommended products</Value>
+ </Resource>
<Resource Name="Products.RelatedProducts">
<Value>Related products</Value>
</Resource>
diff --git a/src/Web/Grand.Web/Components/RecommendedProducts.cs b/src/Web/Grand.Web/Components/RecommendedProducts.cs
new file mode 100644
index 000000000..183e2f81f
--- /dev/null
+++ b/src/Web/Grand.Web/Components/RecommendedProducts.cs
@@ -0,0 +1,61 @@
+using Grand.Business.Catalog.Interfaces.Products;
+using Grand.Domain.Catalog;
+using Grand.Web.Common.Components;
+using Grand.Web.Features.Models.Products;
+using MediatR;
+using Microsoft.AspNetCore.Mvc;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace Grand.Web.Components
+{
+ public class RecommendedProductsViewComponent : BaseViewComponent
+ {
+ #region Fields
+
+ private readonly IProductService _productService;
+ private readonly IMediator _mediator;
+
+ private readonly CatalogSettings _catalogSettings;
+
+ #endregion
+
+ #region Constructors
+ public RecommendedProductsViewComponent(
+ IProductService productService,
+ IMediator mediator,
+ CatalogSettings catalogSettings)
+ {
+ _productService = productService;
+ _mediator = mediator;
+ _catalogSettings = catalogSettings;
+ }
+ #endregion
+
+ #region Invoker
+
+ public async Task<IViewComponentResult> InvokeAsync(string productId, int? productThumbPictureSize)
+ {
+ var productIds = (await _productService.GetProductById(productId)).RecommendedProduct.ToArray();
+ if (!productIds.Any())
+ return Content("");
+
+ var products = await _productService.GetProductsByIds(productIds);
+
+ if (!products.Any())
+ return Content("");
+
+ var model = await _mediator.Send(new GetProductOverview() {
+ PreparePictureModel = true,
+ PreparePriceModel = true,
+ PrepareSpecificationAttributes = _catalogSettings.ShowSpecAttributeOnCatalogPages,
+ ProductThumbPictureSize = productThumbPictureSize,
+ Products = products
+ });
+
+ return View(model);
+ }
+
+ #endregion
+ }
+}
diff --git a/src/Web/Grand.Web/Views/Product/ProductLayout.Simple.cshtml b/src/Web/Grand.Web/Views/Product/ProductLayout.Simple.cshtml
index 27ef59a82..ccc9dd794 100644
--- a/src/Web/Grand.Web/Views/Product/ProductLayout.Simple.cshtml
+++ b/src/Web/Grand.Web/Views/Product/ProductLayout.Simple.cshtml
@@ -108,7 +108,7 @@
<partial name="_Codes" model="Model" />
<!--delivery-->
@if ((Model.FreeShippingNotificationEnabled && Model.IsFreeShipping) || !string.IsNullOrWhiteSpace(Model.DeliveryDate)
- || Model.NotReturnable || Model.AdditionalShippingCharge > 0)
+ || Model.NotReturnable || Model.AdditionalShippingCharge > 0)
{
<partial name="_DeliveryInfo" model="Model" />
}
@@ -246,6 +246,7 @@
<vc:products-also-purchased product-id="@Model.Id" product-thumb-picture-size="null"></vc:products-also-purchased>
<vc:related-products product-id="@Model.Id" product-thumb-picture-size="null"></vc:related-products>
<vc:similar-products product-id="@Model.Id" product-thumb-picture-size="null"></vc:similar-products>
+ <vc:recommended-products product-id="@Model.Id" product-thumb-picture-size="null"></vc:recommended-products>
</div>
</form>
<vc:widget widget-zone="productdetails_bottom" additional-data="@Model.Id"></vc:widget>
diff --git a/src/Web/Grand.Web/Views/Shared/Components/RecommendedProducts/Default.cshtml b/src/Web/Grand.Web/Views/Shared/Components/RecommendedProducts/Default.cshtml
new file mode 100644
index 000000000..7bf8631b6
--- /dev/null
+++ b/src/Web/Grand.Web/Views/Shared/Components/RecommendedProducts/Default.cshtml
@@ -0,0 +1,16 @@
+@model IList<ProductOverviewModel>
+@if (Model.Any())
+{
+ <div class="recommended-products-grid product-grid col-12 px-0">
+ <h2 class="h3 generalTitle text-center">@Loc["Products.RecommendedProducts"]</h2>
+ <div class="form-row">
+ @foreach (var item in Model)
+ {
+ <div class="col-xl-3 col-lg-4 col-md-4 col-sm-6 product-container mb-2">
+ <partial name="_CatalogProductView" model="item" />
+ </div>
+ }
+ </div>
+ </div>
+}
+