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.

Changes with shipping methods (ValidateShippingForm, change separator for shipping option)

support623332418a6a
diff --git a/src/Business/Grand.Business.Core/Interfaces/Checkout/Shipping/IShippingRateCalculationProvider.cs b/src/Business/Grand.Business.Core/Interfaces/Checkout/Shipping/IShippingRateCalculationProvider.cs
index 7ddbbf877..0bab24c7d 100644
--- a/src/Business/Grand.Business.Core/Interfaces/Checkout/Shipping/IShippingRateCalculationProvider.cs
+++ b/src/Business/Grand.Business.Core/Interfaces/Checkout/Shipping/IShippingRateCalculationProvider.cs
@@ -2,7 +2,6 @@ using Grand.Business.Core.Enums.Checkout;
using Grand.Business.Core.Utilities.Checkout;
using Grand.Infrastructure.Plugins;
using Grand.Domain.Orders;
-using Microsoft.AspNetCore.Http;
namespace Grand.Business.Core.Interfaces.Checkout.Shipping
{
@@ -41,9 +40,8 @@ namespace Grand.Business.Core.Interfaces.Checkout.Shipping
/// Gets a shipment tracker
/// </summary>
IShipmentTracker ShipmentTracker { get; }
-
-
- Task<IList<string>> ValidateShippingForm(Dictionary<string, string> model);
+
+ Task<IList<string>> ValidateShippingForm(string shippingOption, IDictionary<string, string> data);
/// <summary>
/// Gets a route name for displaying plugin ("shipping" checkout step)
diff --git a/src/Plugins/Shipping.ByWeight/ByWeightShippingProvider.cs b/src/Plugins/Shipping.ByWeight/ByWeightShippingProvider.cs
index abc5e5e18..6a4de547e 100644
--- a/src/Plugins/Shipping.ByWeight/ByWeightShippingProvider.cs
+++ b/src/Plugins/Shipping.ByWeight/ByWeightShippingProvider.cs
@@ -13,7 +13,6 @@ using Grand.Domain.Customers;
using Grand.Domain.Orders;
using Grand.Domain.Shipping;
using Grand.Infrastructure;
-using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Shipping.ByWeight.Services;
@@ -305,7 +304,7 @@ namespace Shipping.ByWeight
public IList<string> LimitedToGroups => new List<string>();
- public async Task<IList<string>> ValidateShippingForm(Dictionary<string, string> model)
+ public async Task<IList<string>> ValidateShippingForm(string shippingOption, IDictionary<string, string> data)
{
//you can implement here any validation logic
return await Task.FromResult(new List<string>());
diff --git a/src/Plugins/Shipping.FixedRateShipping/FixedRateShippingProvider.cs b/src/Plugins/Shipping.FixedRateShipping/FixedRateShippingProvider.cs
index 8341c7d69..a93fc4596 100644
--- a/src/Plugins/Shipping.FixedRateShipping/FixedRateShippingProvider.cs
+++ b/src/Plugins/Shipping.FixedRateShipping/FixedRateShippingProvider.cs
@@ -8,7 +8,6 @@ using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Domain.Orders;
using Grand.Domain.Shipping;
using Grand.Infrastructure;
-using Microsoft.AspNetCore.Http;
using Shipping.FixedRateShipping.Models;
namespace Shipping.FixedRateShipping
@@ -118,7 +117,7 @@ namespace Shipping.FixedRateShipping
public IShipmentTracker ShipmentTracker => null;
- public async Task<IList<string>> ValidateShippingForm(Dictionary<string, string> model)
+ public async Task<IList<string>> ValidateShippingForm(string shippingOption, IDictionary<string, string> data)
{
return await Task.FromResult(new List<string>());
}
diff --git a/src/Plugins/Shipping.ShippingPoint/Controllers/SelectedShippingPointController.cs b/src/Plugins/Shipping.ShippingPoint/Controllers/SelectedShippingPointController.cs
index 331891f70..a544f60ca 100644
--- a/src/Plugins/Shipping.ShippingPoint/Controllers/SelectedShippingPointController.cs
+++ b/src/Plugins/Shipping.ShippingPoint/Controllers/SelectedShippingPointController.cs
@@ -36,45 +36,37 @@ namespace Shipping.ShippingPoint.Controllers
public async Task<IActionResult> Get(string shippingOptionId)
{
var shippingPoint = await _shippingPointService.GetStoreShippingPointById(shippingOptionId);
- if (shippingPoint != null)
- {
- double rateBase = await _currencyService.ConvertFromPrimaryStoreCurrency(shippingPoint.PickupFee, _workContext.WorkingCurrency);
- var fee = _priceFormatter.FormatShippingPrice(rateBase);
+ if (shippingPoint == null) return Content("ShippingPointController: given Shipping Option doesn't exist");
+ var rateBase = await _currencyService.ConvertFromPrimaryStoreCurrency(shippingPoint.PickupFee, _workContext.WorkingCurrency);
+ var fee = _priceFormatter.FormatShippingPrice(rateBase);
- var viewModel = new PointModel() {
- ShippingPointName = shippingPoint.ShippingPointName,
- Description = shippingPoint.Description,
- PickupFee = fee,
- OpeningHours = shippingPoint.OpeningHours,
- Address1 = shippingPoint.Address1,
- City = shippingPoint.City,
- CountryName = (await _countryService.GetCountryById(shippingPoint.CountryId))?.Name,
- ZipPostalCode = shippingPoint.ZipPostalCode,
- };
- return View(viewModel);
- }
- return Content("ShippingPointController: given Shipping Option doesn't exist");
+ var viewModel = new PointModel() {
+ ShippingPointName = shippingPoint.ShippingPointName,
+ Description = shippingPoint.Description,
+ PickupFee = fee,
+ OpeningHours = shippingPoint.OpeningHours,
+ Address1 = shippingPoint.Address1,
+ City = shippingPoint.City,
+ CountryName = (await _countryService.GetCountryById(shippingPoint.CountryId))?.Name,
+ ZipPostalCode = shippingPoint.ZipPostalCode,
+ };
+ return View(viewModel);
}
public async Task<IActionResult> Points(string shippingOption)
{
- var parameter = shippingOption.Split(new[] { "___" }, StringSplitOptions.RemoveEmptyEntries)[0];
+ var parameter = shippingOption.Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries)[0];
- if (parameter == _translationService.GetResource("Shipping.ShippingPoint.PluginName"))
- {
- var shippingPoints = await _shippingPointService.GetAllStoreShippingPoint(_workContext.CurrentStore.Id);
+ if (parameter != _translationService.GetResource("Shipping.ShippingPoint.PluginName"))
+ return Content("ShippingPointController: given Shipping Option doesn't exist");
+ var shippingPoints = await _shippingPointService.GetAllStoreShippingPoint(_workContext.CurrentStore.Id);
- var shippingPointsModel = new List<SelectListItem>();
- shippingPointsModel.Add(new SelectListItem() { Value = "", Text = _translationService.GetResource("Shipping.ShippingPoint.SelectShippingOption") });
+ var shippingPointsModel = new List<SelectListItem> {
+ new() { Value = "", Text = _translationService.GetResource("Shipping.ShippingPoint.SelectShippingOption") }
+ };
+ shippingPointsModel.AddRange(shippingPoints.Select(shippingPoint => new SelectListItem() { Value = shippingPoint.Id, Text = shippingPoint.ShippingPointName }));
- foreach (var shippingPoint in shippingPoints)
- {
- shippingPointsModel.Add(new SelectListItem() { Value = shippingPoint.Id, Text = shippingPoint.ShippingPointName });
- }
-
- return View(shippingPointsModel);
- }
- return Content("ShippingPointController: given Shipping Option doesn't exist");
+ return View(shippingPointsModel);
}
}
diff --git a/src/Plugins/Shipping.ShippingPoint/ShippingPointRateProvider.cs b/src/Plugins/Shipping.ShippingPoint/ShippingPointRateProvider.cs
index c744ed2da..946eb691c 100644
--- a/src/Plugins/Shipping.ShippingPoint/ShippingPointRateProvider.cs
+++ b/src/Plugins/Shipping.ShippingPoint/ShippingPointRateProvider.cs
@@ -8,7 +8,6 @@ using Grand.Domain.Customers;
using Grand.Domain.Orders;
using Grand.Domain.Shipping;
using Grand.Infrastructure;
-using Microsoft.AspNetCore.Http;
using Shipping.ShippingPoint.Services;
using System.Xml.Serialization;
@@ -91,12 +90,11 @@ namespace Shipping.ShippingPoint
return await Task.FromResult(false);
}
- public async Task<IList<string>> ValidateShippingForm(Dictionary<string, string> model)
+ public async Task<IList<string>> ValidateShippingForm(string shippingOption, IDictionary<string, string> data)
{
- model.TryGetValue("shippingoption", out var shippingOption);
- model.TryGetValue("selectedShippingOption", out var shippingOptionId);
+ data.TryGetValue("selectedShippingOption", out var shippingOptionId);
- var shippingMethodName = shippingOption?.Replace("___", "_").Split(new[] { '_' })[0];
+ var shippingMethodName = shippingOption?.Split(new[] { ':' })[0];
if (string.IsNullOrEmpty(shippingOptionId))
return new List<string>() { _translationService.GetResource("Shipping.ShippingPoint.SelectBeforeProceed") };
diff --git a/src/Plugins/Shipping.ShippingPoint/Views/SelectedShippingPoint/Points.cshtml b/src/Plugins/Shipping.ShippingPoint/Views/SelectedShippingPoint/Points.cshtml
index d9f6a8827..8151694c5 100644
--- a/src/Plugins/Shipping.ShippingPoint/Views/SelectedShippingPoint/Points.cshtml
+++ b/src/Plugins/Shipping.ShippingPoint/Views/SelectedShippingPoint/Points.cshtml
@@ -24,7 +24,7 @@
</div>
<h6>@Loc["Shipping.ShippingPoint.ChooseShippingPoint"]</h6>
-<select class="form-control" name="selectedShippingOption" onchange="renderShippingOptionInfo(value)">
+<select class="form-control" name="data[selectedShippingOption]" onchange="renderShippingOptionInfo(value)">
@foreach (var shippingOption in Model)
{
<option value="@shippingOption.Value">@shippingOption.Text</option>
diff --git a/src/Web/Grand.Web/Controllers/CheckoutController.cs b/src/Web/Grand.Web/Controllers/CheckoutController.cs
index 514a43030..a66149e1f 100644
--- a/src/Web/Grand.Web/Controllers/CheckoutController.cs
+++ b/src/Web/Grand.Web/Controllers/CheckoutController.cs
@@ -117,7 +117,7 @@ namespace Grand.Web.Controllers
[NonAction]
protected IShippingRateCalculationProvider GetShippingComputation(string input)
{
- var shippingMethodName = input.Split(new[] { "___" }, StringSplitOptions.RemoveEmptyEntries)[1];
+ var shippingMethodName = input.Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries)[1];
var shippingMethod = _shippingService.LoadShippingRateCalculationProviderBySystemName(shippingMethodName);
if (shippingMethod == null)
throw new Exception("Shipping method is not selected");
@@ -126,10 +126,10 @@ namespace Grand.Web.Controllers
}
[NonAction]
- private async Task<IList<string>> ValidateShippingForm(IFormCollection form)
+ private async Task<IList<string>> ValidateShippingForm(CheckoutShippingMethodModel model)
{
- var warnings = (await GetShippingComputation(form["shippingoption"])
- .ValidateShippingForm(form.ToDictionary(x=>x.Key, y=>y.Value.ToString()))).ToList();
+ var warnings = (await GetShippingComputation(model.ShippingOption)
+ .ValidateShippingForm(model.ShippingOption, model.Data)).ToList();
foreach (var warning in warnings)
ModelState.AddModelError("", warning);
return warnings;
@@ -780,7 +780,7 @@ namespace Grand.Web.Controllers
}
}
- public virtual async Task<IActionResult> SaveShippingMethod(IFormCollection form)
+ public virtual async Task<IActionResult> SaveShippingMethod(CheckoutShippingMethodModel model)
{
try
{
@@ -796,15 +796,16 @@ namespace Grand.Web.Controllers
throw new Exception("Shipping is not required");
//parse selected method
- string shippingoption = form["shippingoption"];
- if (string.IsNullOrEmpty(shippingoption))
+ //model.TryGetValue("shippingoption", out var shipping);
+ if (string.IsNullOrEmpty(model.ShippingOption))
throw new Exception("Selected shipping method can't be parsed");
- var splittedOption = shippingoption.Split(new[] { "___" }, StringSplitOptions.RemoveEmptyEntries);
- if (splittedOption.Length != 2)
+
+ var splitOption = model.ShippingOption.Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
+ if (splitOption.Length != 2)
throw new Exception("Selected shipping method can't be parsed");
- var selectedName = splittedOption[0];
- var shippingRateProviderSystemName = splittedOption[1];
+ var selectedName = splitOption[0];
+ var shippingRateProviderSystemName = splitOption[1];
//clear shipping option XML/Description
await _userFieldService.SaveField(customer, SystemCustomerFieldNames.ShippingOptionAttribute, "",
@@ -813,7 +814,7 @@ namespace Grand.Web.Controllers
"", store.Id);
//validate customer's input
- var warnings = (await ValidateShippingForm(form)).ToList();
+ var warnings = (await ValidateShippingForm(model)).ToList();
//find it
//performance optimization. try cache first
diff --git a/src/Web/Grand.Web/Models/Checkout/CheckoutShippingMethodModel.cs b/src/Web/Grand.Web/Models/Checkout/CheckoutShippingMethodModel.cs
index ae582cc96..65ae819f4 100644
--- a/src/Web/Grand.Web/Models/Checkout/CheckoutShippingMethodModel.cs
+++ b/src/Web/Grand.Web/Models/Checkout/CheckoutShippingMethodModel.cs
@@ -13,6 +13,8 @@ namespace Grand.Web.Models.Checkout
public IList<ShippingMethodModel> ShippingMethods { get; set; }
+ public string ShippingOption { get; set; }
+ public IDictionary<string, string> Data{ get; set; }
public IList<string> Warnings { get; set; }
#region Nested classes
diff --git a/src/Web/Grand.Web/Views/Checkout/Partials/ShippingMethods.cshtml b/src/Web/Grand.Web/Views/Checkout/Partials/ShippingMethods.cshtml
index d1ae6495b..9c80ef9a1 100644
--- a/src/Web/Grand.Web/Views/Checkout/Partials/ShippingMethods.cshtml
+++ b/src/Web/Grand.Web/Views/Checkout/Partials/ShippingMethods.cshtml
@@ -10,7 +10,7 @@
@@click="vmorder.selectedShippingMethod = index; vmorder.ShippingMethodError = undefined"
name="shippingoption"
:data-order="index"
- :value="shippingMethod.Name + '___' + shippingMethod.ShippingRateProviderSystemName"
+ :value="shippingMethod.Name + ':' + shippingMethod.ShippingRateProviderSystemName"
:checked="shippingMethod.Selected"
class="custom-control-input">
<span class="custom-control-label"></span>
diff --git a/src/Web/Grand.Web/wwwroot/theme/script/public.checkout.js b/src/Web/Grand.Web/wwwroot/theme/script/public.checkout.js
index c4bb1d600..9f02d7de2 100644
--- a/src/Web/Grand.Web/wwwroot/theme/script/public.checkout.js
+++ b/src/Web/Grand.Web/wwwroot/theme/script/public.checkout.js
@@ -219,7 +219,7 @@ var vmorder = new Vue({
} else {
index = 0;
}
- var elem = model.ShippingMethods[index].Name + '___' + model.ShippingMethods[index].ShippingRateProviderSystemName;
+ var elem = model.ShippingMethods[index].Name + ':' + model.ShippingMethods[index].ShippingRateProviderSystemName;
vmorder.loadPartialView(elem);
}
vmorder.updateTotals();