protected async Task <CheckoutAddressModel> PrepareCheckoutAddressModelAsync(bool shipping = false, int?selectedCountryId = null) { // Get existing addresses. var customer = Services.WorkContext.CurrentCustomer; var addresses = new List <Address>(customer.Addresses).AsEnumerable(); // And map billing / shipping addresses. var model = new CheckoutAddressModel(); await addresses.MapAsync(model, shipping, selectedCountryId); return(model); }
public ActionResult ConfirmAddress() { if (_workContext.CurrentProfile.IsAnonymous) { return(new HttpUnauthorizedResult()); } var profileId = _workContext.CurrentProfile.Id; // Check if customer needs to fill in a pharmaceutical form var needToFillForm = _cartService.CheckIfNeedPharmForm(profileId); if (needToFillForm) { return(RedirectToRoute("Checkout Pharm Form")); } var allAddresses = _accountService.GetAddressesByProfileId(profileId); var billing = allAddresses.Where(x => x.IsBilling == true).FirstOrDefault(); var shipping = allAddresses.Where(x => x.IsShipping == true).FirstOrDefault(); var hasOnlyFreeNHSPrescriptionItem = _cartService.HasOnlyFreeNHSPrescriptionItem(profileId); // If we have only 1 item which is a NHS Prescription, then we do not need to have billing address var needBillingAddress = !hasOnlyFreeNHSPrescriptionItem; // For FOC order, do not display billing address var orderTotals = _cartService.CalculateOrderTotals(profileId); if (orderTotals.Total <= 0M) { needBillingAddress = false; } if (billing == null && shipping == null) { // Choose a default address if (allAddresses.Count > 0) { billing = allAddresses[0]; shipping = allAddresses[0]; _accountService.UpdatePrimaryBillingAddress(billing.Id, profileId: profileId); _accountService.UpdatePrimaryShippingAddress(shipping.Id, profileId: profileId); } else { return(RedirectToRoute("Checkout New Address", new { type = Convert.ToInt32(AddressType.Both) })); } } var model = new CheckoutAddressModel { NeedBillingAddress = needBillingAddress, HasSavedAddress = allAddresses.Count > 0 }; if (needBillingAddress && billing != null) { model.BillingAddress = billing.PrepareAddressModel(); model.HasBillingAddress = true; } if (shipping != null) { model.ShippingAddress = shipping.PrepareAddressModel(); model.HasShippingAddress = true; // Current selected shipping option country has to match shipping address country. if (string.Compare(model.ShippingAddress.CountryId, _workContext.CurrentCountry.Id.ToString()) != 0) { model.HasShippingAddress = false; model.DisableProceed = true; ViewBag.ErrorMessage = string.Format("Please change your country in <i>shipping address</i> to {0}. Alternatively, you may change your shipping country at <a href='{1}'>basket page</a>.", _workContext.CurrentCountry.Name, Url.RouteUrl("Shopping Cart")); } var messages = _cartService.ProcessPostalRestrictionRules(profileId, _workContext.CurrentCountry.ISO3166Code); if (messages.Length > 0) { model.DisableProceed = true; ViewBag.ErrorMessage = string.Join("<br/>", messages); } } if ((needBillingAddress && billing == null) || shipping == null) { model.DisableProceed = true; } return(View(model)); }