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.

Sidebar wishlist - prepare new models / action / endpoint / request handler

KrzysztofPajak842244045f47
diff --git a/src/Web/Grand.Web/Controllers/WishlistController.cs b/src/Web/Grand.Web/Controllers/WishlistController.cs
index c9f841cb4..ea8ebf135 100644
--- a/src/Web/Grand.Web/Controllers/WishlistController.cs
+++ b/src/Web/Grand.Web/Controllers/WishlistController.cs
@@ -61,6 +61,28 @@ namespace Grand.Web.Controllers
#region Wishlist
+
+ public async Task<IActionResult> SidebarWishlist()
+ {
+ if (!await _permissionService.Authorize(StandardPermission.EnableWishlist))
+ return Content("");
+
+ var cart = _workContext.CurrentCustomer.ShoppingCartItems.Where(sci => sci.ShoppingCartTypeId == ShoppingCartType.Wishlist);
+
+ if (!string.IsNullOrEmpty(_workContext.CurrentStore.Id))
+ cart = cart.LimitPerStore(_shoppingCartSettings.SharedCartBetweenStores, _workContext.CurrentStore.Id);
+
+ var model = await _mediator.Send(new GetMiniWishlist() {
+ Cart = cart.ToList(),
+ Customer = _workContext.CurrentCustomer,
+ Language = _workContext.WorkingLanguage,
+ Currency = _workContext.WorkingCurrency,
+ Store = _workContext.CurrentStore,
+ });
+
+ return Json(model);
+ }
+
[HttpGet]
public virtual async Task<IActionResult> Index(Guid? customerGuid)
{
diff --git a/src/Web/Grand.Web/Endpoints/EndpointProvider.cs b/src/Web/Grand.Web/Endpoints/EndpointProvider.cs
index 90e3258be..6f32c6983 100644
--- a/src/Web/Grand.Web/Endpoints/EndpointProvider.cs
+++ b/src/Web/Grand.Web/Endpoints/EndpointProvider.cs
@@ -590,6 +590,11 @@ namespace Grand.Web.Endpoints
pattern + "getitemcart/{shoppingcartId?}",
new { controller = "ActionCart", action = "GetItemCart" });
+ //side bar wishlist
+ endpointRouteBuilder.MapControllerRoute("SidebarWishlist",
+ $"{pattern}sidebarwishlist/",
+ new { controller = "Wishlist", action = "SidebarWishlist" });
+
//wishlist
endpointRouteBuilder.MapControllerRoute("Wishlist",
pattern + "wishlist/{customerGuid?}",
diff --git a/src/Web/Grand.Web/Features/Handlers/ShoppingCart/GetMiniWishlistHandler.cs b/src/Web/Grand.Web/Features/Handlers/ShoppingCart/GetMiniWishlistHandler.cs
new file mode 100644
index 000000000..cac86c462
--- /dev/null
+++ b/src/Web/Grand.Web/Features/Handlers/ShoppingCart/GetMiniWishlistHandler.cs
@@ -0,0 +1,137 @@
+using Grand.Business.Catalog.Extensions;
+using Grand.Business.Catalog.Interfaces.Prices;
+using Grand.Business.Catalog.Interfaces.Products;
+using Grand.Business.Catalog.Interfaces.Tax;
+using Grand.Business.Common.Extensions;
+using Grand.Business.Common.Interfaces.Localization;
+using Grand.Business.Storage.Interfaces;
+using Grand.Domain.Catalog;
+using Grand.Domain.Common;
+using Grand.Domain.Media;
+using Grand.Domain.Orders;
+using Grand.Web.Features.Models.ShoppingCart;
+using Grand.Web.Models.Media;
+using Grand.Web.Models.ShoppingCart;
+using MediatR;
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Routing;
+
+namespace Grand.Web.Features.Handlers.ShoppingCart
+{
+ public class GetMiniWishlistHandler : IRequestHandler<GetMiniWishlist, MiniWishlistModel>
+ {
+ private readonly IProductService _productService;
+ private readonly IProductAttributeParser _productAttributeParser;
+ private readonly IProductAttributeFormatter _productAttributeFormatter;
+ private readonly ITranslationService _translationService;
+ private readonly ITaxService _taxService;
+ private readonly IPricingService _pricingService;
+ private readonly IPriceFormatter _priceFormatter;
+ private readonly IPictureService _pictureService;
+ private readonly IHttpContextAccessor _httpContextAccessor;
+ private readonly LinkGenerator _linkGenerator;
+ private readonly ShoppingCartSettings _shoppingCartSettings;
+ private readonly MediaSettings _mediaSettings;
+
+ public GetMiniWishlistHandler(
+ IProductService productService,
+ IProductAttributeParser productAttributeParser,
+ IProductAttributeFormatter productAttributeFormatter,
+ ITranslationService translationService,
+ ITaxService taxService,
+ IPricingService priceCalculationService,
+ IPriceFormatter priceFormatter,
+ IPictureService pictureService,
+ IHttpContextAccessor httpContextAccessor,
+ LinkGenerator linkGenerator,
+ ShoppingCartSettings shoppingCartSettings,
+ MediaSettings mediaSettings)
+ {
+ _productService = productService;
+ _productAttributeParser = productAttributeParser;
+ _productAttributeFormatter = productAttributeFormatter;
+ _translationService = translationService;
+ _taxService = taxService;
+ _pricingService = priceCalculationService;
+ _priceFormatter = priceFormatter;
+ _pictureService = pictureService;
+ _httpContextAccessor = httpContextAccessor;
+ _linkGenerator = linkGenerator;
+ _shoppingCartSettings = shoppingCartSettings;
+ _mediaSettings = mediaSettings;
+ }
+
+ public async Task<MiniWishlistModel> Handle(GetMiniWishlist request, CancellationToken cancellationToken)
+ {
+ var model = new MiniWishlistModel {
+ EmailWishlistEnabled = _shoppingCartSettings.EmailWishlistEnabled,
+ ShowProductImages = _shoppingCartSettings.ShowProductImagesOnWishList,
+ TotalProducts = request.Cart.Count
+ };
+
+ if (!request.Cart.Any())
+ return model;
+
+ #region Cart items
+
+ foreach (var sci in request.Cart)
+ {
+ var product = await _productService.GetProductById(sci.ProductId);
+ if (product == null)
+ continue;
+
+ var sename = product.GetSeName(request.Language.Id);
+ var cartItemModel = new MiniWishlistModel.WishlistItemModel {
+ Id = sci.Id,
+ Sku = product.FormatSku(sci.Attributes, _productAttributeParser),
+ ProductId = product.Id,
+ ProductName = product.GetTranslation(x => x.Name, request.Language.Id),
+ ProductSeName = sename,
+ ProductUrl = _linkGenerator.GetUriByRouteValues(_httpContextAccessor.HttpContext, "Product", new { SeName = sename }),
+ Quantity = sci.Quantity,
+ AttributeInfo = await _productAttributeFormatter.FormatAttributes(product, sci.Attributes),
+ };
+
+ //unit prices
+ if (product.CallForPrice)
+ {
+ cartItemModel.UnitPrice = _translationService.GetResource("Products.CallForPrice");
+ }
+ else
+ {
+ var productprice = await _taxService.GetProductPrice(product, (await _pricingService.GetUnitPrice(sci, product)).unitprice);
+
+ cartItemModel.UnitPrice = _priceFormatter.FormatPrice(productprice.productprice);
+ cartItemModel.UnitPriceValue = productprice.productprice;
+ cartItemModel.TaxRate = productprice.taxRate;
+ }
+
+ //picture
+ if (_shoppingCartSettings.ShowProductImagesOnWishList)
+ {
+ cartItemModel.Picture = await PrepareCartItemPicture(product, sci.Attributes);
+ }
+
+
+ model.Items.Add(cartItemModel);
+ }
+
+ #endregion
+
+ return model;
+ }
+
+ private async Task<PictureModel> PrepareCartItemPicture(
+ Product product, IList<CustomAttribute> attributes)
+ {
+ var sciPicture = await product.GetProductPicture(attributes, _productService, _pictureService, _productAttributeParser);
+ return new PictureModel {
+ Id = sciPicture?.Id,
+ ImageUrl = await _pictureService.GetPictureUrl(sciPicture, _mediaSettings.MiniCartThumbPictureSize, true),
+ Title = string.Format(_translationService.GetResource("Media.Product.ImageLinkTitleFormat"), product.Name),
+ AlternateText = string.Format(_translationService.GetResource("Media.Product.ImageAlternateTextFormat"), product.Name),
+ };
+ }
+
+ }
+}
diff --git a/src/Web/Grand.Web/Features/Models/ShoppingCart/GetMiniWishlist.cs b/src/Web/Grand.Web/Features/Models/ShoppingCart/GetMiniWishlist.cs
new file mode 100644
index 000000000..48389787f
--- /dev/null
+++ b/src/Web/Grand.Web/Features/Models/ShoppingCart/GetMiniWishlist.cs
@@ -0,0 +1,19 @@
+using Grand.Domain.Customers;
+using Grand.Domain.Directory;
+using Grand.Domain.Localization;
+using Grand.Domain.Orders;
+using Grand.Domain.Stores;
+using Grand.Web.Models.ShoppingCart;
+using MediatR;
+
+namespace Grand.Web.Features.Models.ShoppingCart
+{
+ public class GetMiniWishlist : IRequest<MiniWishlistModel>
+ {
+ public Customer Customer { get; set; }
+ public Language Language { get; set; }
+ public Currency Currency { get; set; }
+ public Store Store { get; set; }
+ public IList<ShoppingCartItem> Cart { get; set; }
+ }
+}
diff --git a/src/Web/Grand.Web/Models/ShoppingCart/MiniWishlistModel.cs b/src/Web/Grand.Web/Models/ShoppingCart/MiniWishlistModel.cs
new file mode 100644
index 000000000..93e18c0e0
--- /dev/null
+++ b/src/Web/Grand.Web/Models/ShoppingCart/MiniWishlistModel.cs
@@ -0,0 +1,49 @@
+using Grand.Infrastructure.Models;
+using Grand.Web.Models.Media;
+
+namespace Grand.Web.Models.ShoppingCart
+{
+ public partial class MiniWishlistModel : BaseModel
+ {
+ public MiniWishlistModel()
+ {
+ Items = new List<WishlistItemModel>();
+ }
+
+ public IList<WishlistItemModel> Items { get; set; }
+ public int TotalProducts { get; set; }
+ public bool ShowProductImages { get; set; }
+
+ public bool EmailWishlistEnabled { get; set; }
+
+ #region Nested Classes
+
+ public partial class WishlistItemModel : BaseEntityModel
+ {
+ public WishlistItemModel()
+ {
+ Picture = new PictureModel();
+ }
+
+ public string ProductId { get; set; }
+
+ public string ProductName { get; set; }
+
+ public string ProductSeName { get; set; }
+ public string ProductUrl { get; set; }
+ public string Sku { get; set; }
+
+ public int Quantity { get; set; }
+
+ public string UnitPrice { get; set; }
+ public double UnitPriceValue { get; set; }
+ public double TaxRate { get; set; }
+
+ public string AttributeInfo { get; set; }
+
+ public PictureModel Picture { get; set; }
+ }
+
+ #endregion
+ }
+}
\ No newline at end of file
diff --git a/src/Web/Grand.Web/Views/Shared/SidebarWishlist.cshtml b/src/Web/Grand.Web/Views/Shared/SidebarWishlist.cshtml
index 5043a6c6d..03b36ae24 100644
--- a/src/Web/Grand.Web/Views/Shared/SidebarWishlist.cshtml
+++ b/src/Web/Grand.Web/Views/Shared/SidebarWishlist.cshtml
@@ -7,7 +7,7 @@
<ul class="sidebar-cart wishlist px-0">
<li class="nav-item">
<a href="#" target="_self" class="nav-link">
- <b-icon icon="heart" :variant="[darkMode ? 'white' : 'dark']" @@click="updateWishlist('@Url.RouteUrl("Wishlist")')" v-b-toggle.sidebar-wishlist></b-icon>
+ <b-icon icon="heart" :variant="[darkMode ? 'white' : 'dark']" @@click="updateWishlist('@Url.RouteUrl("SidebarWishlist")')" v-b-toggle.sidebar-wishlist></b-icon>
<span class="qty-indicator wishlist-qty" ref="wishlistQty">
@Loc["Wishlist.HeaderQuantity", Model]
</span>
@@ -17,7 +17,7 @@
<li>
<b-sidebar id="sidebar-wishlist" backdrop @if (!supportRtl) { <text>right</text> }>
<template v-slot:title v-if="wishindicator > 0">
- @Html.Raw(string.Format(Loc["Wishlist.Mini.ItemsText"], string.Format("<a href=\"{0}\">{1}</a>", Url.RouteUrl("ShoppingCart"), string.Format(Loc["Wishlist.Mini.Items"], "{{wishindicator}}"))))
+ @Html.Raw(string.Format(Loc["Wishlist.Mini.ItemsText"], string.Format("<a href=\"{0}\">{1}</a>", Url.RouteUrl("Wishlist"), string.Format(Loc["Wishlist.Mini.Items"], "{{wishindicator}}"))))
</template>
<template v-slot:title v-else>
@Loc["Wishlist.Mini.NoItems"]
diff --git a/src/Web/Grand.Web/wwwroot/theme/script/app.js b/src/Web/Grand.Web/wwwroot/theme/script/app.js
index 83483a0e2..5dc259e62 100644
--- a/src/Web/Grand.Web/wwwroot/theme/script/app.js
+++ b/src/Web/Grand.Web/wwwroot/theme/script/app.js
@@ -250,8 +250,7 @@ var vm = new Vue({
data: null,
headers: {
'Accept': 'application/json',
- 'Content-Type': 'application/json',
- 'X-Response-View': 'Json'
+ 'Content-Type': 'application/json',
}
}).then(response => (
this.loader = false,