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.

Minor changes - Checkout- addresses

KrzysztofPajak269170e0a85c
diff --git a/src/Web/Grand.Web/Controllers/CheckoutController.cs b/src/Web/Grand.Web/Controllers/CheckoutController.cs
index 22590fc8e..c7c6def1c 100644
--- a/src/Web/Grand.Web/Controllers/CheckoutController.cs
+++ b/src/Web/Grand.Web/Controllers/CheckoutController.cs
@@ -136,6 +136,7 @@ namespace Grand.Web.Controllers
ModelState.AddModelError("", warning);
return warnings;
}
+
private async Task<CheckoutPaymentMethodModel> GetCheckoutPaymentMethodModel(IList<ShoppingCartItem> cart)
{
var filterByCountryId = "";
@@ -238,7 +239,7 @@ namespace Grand.Web.Controllers
return Challenge();
Order order = null;
- if (!String.IsNullOrEmpty(orderId))
+ if (!string.IsNullOrEmpty(orderId))
{
order = await _orderService.GetOrderById(orderId);
}
@@ -308,7 +309,6 @@ namespace Grand.Web.Controllers
return await LoadStepAfterShippingMethod(cart);
}
-
return Json(new
{
update_section = new UpdateSectionJsonModel
@@ -325,14 +325,14 @@ namespace Grand.Web.Controllers
{
//Check whether payment workflow is required
//we ignore loyalty points during cart total calculation
- bool isPaymentWorkflowRequired = await _mediator.Send(new GetIsPaymentWorkflowRequired() { Cart = cart, UseLoyaltyPoints = false });
+ var isPaymentWorkflowRequired = await _mediator.Send(new GetIsPaymentWorkflowRequired() { Cart = cart, UseLoyaltyPoints = false });
if (isPaymentWorkflowRequired)
{
//filter by country
- string filterByCountryId = "";
+ var filterByCountryId = "";
if (_addressSettings.CountryEnabled &&
_workContext.CurrentCustomer.BillingAddress != null &&
- !String.IsNullOrEmpty(_workContext.CurrentCustomer.BillingAddress.CountryId))
+ !string.IsNullOrEmpty(_workContext.CurrentCustomer.BillingAddress.CountryId))
{
filterByCountryId = _workContext.CurrentCustomer.BillingAddress.CountryId;
}
@@ -497,14 +497,11 @@ namespace Grand.Web.Controllers
string billingAddressId = form["billing_address_id"];
- if (!String.IsNullOrEmpty(billingAddressId))
+ if (!string.IsNullOrEmpty(billingAddressId))
{
//existing address
var address = _workContext.CurrentCustomer.Addresses.FirstOrDefault(a => a.Id == billingAddressId);
- if (address == null)
- throw new Exception("Address can't be loaded");
-
- _workContext.CurrentCustomer.BillingAddress = address;
+ _workContext.CurrentCustomer.BillingAddress = address ?? throw new Exception("Address can't be loaded");
await _customerService.UpdateBillingAddress(address, _workContext.CurrentCustomer.Id);
}
else
@@ -652,7 +649,7 @@ namespace Grand.Web.Controllers
if (!pickupInstore)
{
- if (!String.IsNullOrEmpty(shippingAddressId))
+ if (!string.IsNullOrEmpty(shippingAddressId))
{
//existing address
var address = _workContext.CurrentCustomer.Addresses.FirstOrDefault(a => a.Id == shippingAddressId);
@@ -676,12 +673,7 @@ namespace Grand.Web.Controllers
}
if (!ModelState.IsValid)
- {
- var errors = ModelState.Values.SelectMany(v => v.Errors);
- foreach (var item in errors)
- {
- string tt = item.ErrorMessage;
- }
+ {
//model is not valid. redisplay the form with errors
var shippingAddressModel = await _mediator.Send(new GetShippingAddress()
{
@@ -728,8 +720,8 @@ namespace Grand.Web.Controllers
}
}
-
- if (model.BillToTheSameAddress && !pickupInstore)
+ if (model.BillToTheSameAddress && !pickupInstore &&
+ (_workContext.CurrentCustomer.ShippingAddress.AddressType != AddressType.Shipping))
{
_workContext.CurrentCustomer.BillingAddress = _workContext.CurrentCustomer.ShippingAddress;
await _customerService.UpdateBillingAddress(_workContext.CurrentCustomer.BillingAddress, _workContext.CurrentCustomer.Id);
@@ -761,7 +753,6 @@ namespace Grand.Web.Controllers
});
}
- //return await LoadStepAfterShippingAddress(cart);
}
catch (Exception exc)
{
@@ -791,8 +782,9 @@ namespace Grand.Web.Controllers
var splittedOption = shippingoption.Split(new[] { "___" }, StringSplitOptions.RemoveEmptyEntries);
if (splittedOption.Length != 2)
throw new Exception("Selected shipping method can't be parsed");
- string selectedName = splittedOption[0];
- string shippingRateProviderSystemName = splittedOption[1];
+
+ var selectedName = splittedOption[0];
+ var shippingRateProviderSystemName = splittedOption[1];
//clear shipping option XML/Description
await _userFieldService.SaveField(customer, SystemCustomerFieldNames.ShippingOptionAttribute, "", store.Id);
@@ -833,7 +825,7 @@ namespace Grand.Web.Controllers
return await LoadStepAfterShippingMethod(cart);
}
- var message = String.Join(", ", warnings.ToArray());
+ var message = string.Join(", ", warnings.ToArray());
return Json(new { error = 1, message = message });
}
catch (Exception exc)
@@ -1049,7 +1041,6 @@ namespace Grand.Web.Controllers
if ((await _groupService.IsGuest(_workContext.CurrentCustomer) && !_orderSettings.AnonymousCheckoutAllowed))
return Challenge();
-
var paymentTransaction = await _paymentTransactionService.GetById(paymentTransactionId);
if (paymentTransaction == null)
return RedirectToRoute("HomePage");
@@ -1085,7 +1076,5 @@ namespace Grand.Web.Controllers
return Content(exc.Message);
}
}
-
-
}
}
diff --git a/src/Web/Grand.Web/Features/Handlers/Checkout/GetBillingAddressHandler.cs b/src/Web/Grand.Web/Features/Handlers/Checkout/GetBillingAddressHandler.cs
index 41dd5bbfe..5c9635b94 100644
--- a/src/Web/Grand.Web/Features/Handlers/Checkout/GetBillingAddressHandler.cs
+++ b/src/Web/Grand.Web/Features/Handlers/Checkout/GetBillingAddressHandler.cs
@@ -1,9 +1,6 @@
-using Grand.Business.Checkout.Extensions;
-using Grand.Business.Common.Interfaces.Directory;
+using Grand.Business.Common.Interfaces.Directory;
using Grand.Business.Common.Interfaces.Security;
-using Grand.Business.Common.Interfaces.Stores;
using Grand.Domain.Common;
-using Grand.Domain.Shipping;
using Grand.Web.Features.Models.Checkout;
using Grand.Web.Features.Models.Common;
using Grand.Web.Models.Checkout;
@@ -17,20 +14,21 @@ namespace Grand.Web.Features.Handlers.Checkout
{
public class GetBillingAddressHandler : IRequestHandler<GetBillingAddress, CheckoutBillingAddressModel>
{
- private readonly ShippingSettings _shippingSettings;
private readonly ICountryService _countryService;
private readonly IAclService _aclService;
private readonly IMediator _mediator;
+ private readonly AddressSettings _addressSettings;
- public GetBillingAddressHandler(ShippingSettings shippingSettings,
+ public GetBillingAddressHandler(
ICountryService countryService,
IAclService aclService,
- IMediator mediator)
+ IMediator mediator,
+ AddressSettings addressSettings)
{
- _shippingSettings = shippingSettings;
_countryService = countryService;
_aclService = aclService;
_mediator = mediator;
+ _addressSettings = addressSettings;
}
public async Task<CheckoutBillingAddressModel> Handle(GetBillingAddress request, CancellationToken cancellationToken)
@@ -38,7 +36,7 @@ namespace Grand.Web.Features.Handlers.Checkout
var model = new CheckoutBillingAddressModel();
//existing addresses
var addresses = new List<Address>();
- foreach (var item in request.Customer.Addresses.Where(x=>x.AddressType == AddressType.Any || x.AddressType == AddressType.Billing))
+ foreach (var item in request.Customer.Addresses.Where(x => x.AddressType == AddressType.Any || x.AddressType == AddressType.Billing))
{
if (string.IsNullOrEmpty(item.CountryId))
{
@@ -55,8 +53,7 @@ namespace Grand.Web.Features.Handlers.Checkout
foreach (var address in addresses)
{
- var addressModel = await _mediator.Send(new GetAddressModel()
- {
+ var addressModel = await _mediator.Send(new GetAddressModel() {
Language = request.Language,
Store = request.Store,
Model = null,
@@ -70,8 +67,7 @@ namespace Grand.Web.Features.Handlers.Checkout
model.NewAddress.CountryId = request.SelectedCountryId;
var countries = await _countryService.GetAllCountriesForBilling(request.Language.Id, request.Store.Id);
- model.NewAddress = await _mediator.Send(new GetAddressModel()
- {
+ model.NewAddress = await _mediator.Send(new GetAddressModel() {
Language = request.Language,
Store = request.Store,
Model = model.NewAddress,
@@ -82,6 +78,7 @@ namespace Grand.Web.Features.Handlers.Checkout
Customer = request.Customer,
OverrideAttributes = request.OverrideAttributes
});
+ model.NewAddress.AddressTypeId = _addressSettings.AddressTypeEnabled ? (int)AddressType.Billing : (int)AddressType.Any;
return model;
}
diff --git a/src/Web/Grand.Web/Features/Handlers/Checkout/GetShippingAddressHandler.cs b/src/Web/Grand.Web/Features/Handlers/Checkout/GetShippingAddressHandler.cs
index a280052c3..18fa74fd0 100644
--- a/src/Web/Grand.Web/Features/Handlers/Checkout/GetShippingAddressHandler.cs
+++ b/src/Web/Grand.Web/Features/Handlers/Checkout/GetShippingAddressHandler.cs
@@ -4,7 +4,6 @@ using Grand.Business.Checkout.Interfaces.Shipping;
using Grand.Business.Common.Interfaces.Directory;
using Grand.Business.Common.Interfaces.Localization;
using Grand.Business.Common.Interfaces.Security;
-using Grand.Business.Common.Interfaces.Stores;
using Grand.Domain.Common;
using Grand.Domain.Shipping;
using Grand.Web.Features.Models.Checkout;
@@ -30,6 +29,7 @@ namespace Grand.Web.Features.Handlers.Checkout
private readonly IAclService _aclService;
private readonly IMediator _mediator;
private readonly ShippingSettings _shippingSettings;
+ private readonly AddressSettings _addressSettings;
public GetShippingAddressHandler(
IShippingService shippingService,
@@ -41,7 +41,8 @@ namespace Grand.Web.Features.Handlers.Checkout
ICountryService countryService,
IAclService aclService,
IMediator mediator,
- ShippingSettings shippingSettings)
+ ShippingSettings shippingSettings,
+ AddressSettings addressSettings)
{
_shippingService = shippingService;
_pickupPointService = pickupPointService;
@@ -53,6 +54,7 @@ namespace Grand.Web.Features.Handlers.Checkout
_aclService = aclService;
_mediator = mediator;
_shippingSettings = shippingSettings;
+ _addressSettings = addressSettings;
}
public async Task<CheckoutShippingAddressModel> Handle(GetShippingAddress request, CancellationToken cancellationToken)
@@ -71,8 +73,7 @@ namespace Grand.Web.Features.Handlers.Checkout
//new address
model.NewAddress.CountryId = request.SelectedCountryId;
var countries = await _countryService.GetAllCountriesForShipping(request.Language.Id, request.Store.Id);
- model.NewAddress = await _mediator.Send(new GetAddressModel()
- {
+ model.NewAddress = await _mediator.Send(new GetAddressModel() {
Language = request.Language,
Store = request.Store,
Model = model.NewAddress,
@@ -83,6 +84,8 @@ namespace Grand.Web.Features.Handlers.Checkout
Customer = request.Customer,
OverrideAttributes = request.OverrideAttributes,
});
+ model.NewAddress.AddressTypeId = _addressSettings.AddressTypeEnabled ? (int)AddressType.Shipping : (int)AddressType.Any;
+
return model;
}
@@ -94,8 +97,7 @@ namespace Grand.Web.Features.Handlers.Checkout
{
foreach (var pickupPoint in pickupPoints)
{
- var pickupPointModel = new CheckoutPickupPointModel()
- {
+ var pickupPointModel = new CheckoutPickupPointModel() {
Id = pickupPoint.Id,
Name = pickupPoint.Name,
Description = pickupPoint.Description,
@@ -143,8 +145,7 @@ namespace Grand.Web.Features.Handlers.Checkout
}
foreach (var address in addresses)
{
- var addressModel = await _mediator.Send(new GetAddressModel()
- {
+ var addressModel = await _mediator.Send(new GetAddressModel() {
Language = request.Language,
Store = request.Store,
Model = null,