public ActionResult NewBillingAddress(CheckoutBillingAddressModel model) { //validation var cart = _workContext.CurrentCustomer.ShoppingCartItems .Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart) .Where(sci => sci.StoreId == _storeContext.CurrentStore.Id) .ToList(); if (cart.Count == 0) return RedirectToRoute("ShoppingCart"); if (UseOnePageCheckout()) return RedirectToRoute("CheckoutOnePage"); if ((_workContext.CurrentCustomer.IsGuest() && !_orderSettings.AnonymousCheckoutAllowed)) return new HttpUnauthorizedResult(); if (ModelState.IsValid) { var address = model.NewAddress.ToEntity(); address.CreatedOnUtc = DateTime.UtcNow; //some validation if (address.CountryId == 0) address.CountryId = null; if (address.StateProvinceId == 0) address.StateProvinceId = null; _workContext.CurrentCustomer.Addresses.Add(address); _workContext.CurrentCustomer.BillingAddress = address; _customerService.UpdateCustomer(_workContext.CurrentCustomer); return RedirectToRoute("CheckoutShippingAddress"); } //If we got this far, something failed, redisplay form model = PrepareBillingAddressModel(model.NewAddress.CountryId); return View(model); }
public ActionResult OpcSaveBilling(FormCollection form) { try { //validation var cart = _workContext.CurrentCustomer.ShoppingCartItems .Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart) .Where(sci => sci.StoreId == _storeContext.CurrentStore.Id) .ToList(); if (cart.Count == 0) throw new Exception("Your cart is empty"); if (!UseOnePageCheckout()) throw new Exception("One page checkout is disabled"); if ((_workContext.CurrentCustomer.IsGuest() && !_orderSettings.AnonymousCheckoutAllowed)) throw new Exception("Anonymous checkout is not allowed"); int billingAddressId = 0; int.TryParse(form["billing_address_id"], out billingAddressId); if (billingAddressId > 0) { //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; _customerService.UpdateCustomer(_workContext.CurrentCustomer); } else { //new address var model = new CheckoutBillingAddressModel(); TryUpdateModel(model.NewAddress, "BillingNewAddress"); //validate model TryValidateModel(model.NewAddress); if (!ModelState.IsValid) { //model is not valid. redisplay the form with errors var billingAddressModel = PrepareBillingAddressModel(model.NewAddress.CountryId); billingAddressModel.NewAddressPreselected = true; return Json(new { update_section = new UpdateSectionJsonModel() { name = "billing", html = this.RenderPartialViewToString("OpcBillingAddress", billingAddressModel) } }); } //try to find an address with the same values (don't duplicate records) var address = _workContext.CurrentCustomer.Addresses.ToList().FindAddress( model.NewAddress.FirstName, model.NewAddress.LastName, model.NewAddress.PhoneNumber, model.NewAddress.Email, model.NewAddress.FaxNumber, model.NewAddress.Company, model.NewAddress.Address1, model.NewAddress.Address2, model.NewAddress.City, model.NewAddress.StateProvinceId, model.NewAddress.ZipPostalCode, model.NewAddress.CountryId); if (address == null) { //address is not found. let's create a new one address = model.NewAddress.ToEntity(); address.CreatedOnUtc = DateTime.UtcNow; //some validation if (address.CountryId == 0) address.CountryId = null; if (address.StateProvinceId == 0) address.StateProvinceId = null; _workContext.CurrentCustomer.Addresses.Add(address); } _workContext.CurrentCustomer.BillingAddress = address; _customerService.UpdateCustomer(_workContext.CurrentCustomer); } if (cart.RequiresShipping()) { //shipping is required var shippingAddressModel = PrepareShippingAddressModel(); return Json(new { update_section = new UpdateSectionJsonModel() { name = "shipping", html = this.RenderPartialViewToString("OpcShippingAddress", shippingAddressModel) }, goto_section = "shipping" }); } else { //shipping is not required _genericAttributeService.SaveAttribute<ShippingOption>(_workContext.CurrentCustomer, SystemCustomerAttributeNames.SelectedShippingOption, null, _storeContext.CurrentStore.Id); //Check whether payment workflow is required //we ignore reward points during cart total calculation bool isPaymentWorkflowRequired = IsPaymentWorkflowRequired(cart, true); if (isPaymentWorkflowRequired) { //payment is required var paymentMethodModel = PreparePaymentMethodModel(cart); if (_paymentSettings.BypassPaymentMethodSelectionIfOnlyOne && paymentMethodModel.PaymentMethods.Count == 1 && !paymentMethodModel.DisplayRewardPoints) { //if we have only one payment method and reward points are disabled or the current customer doesn't have any reward points //so customer doesn't have to choose a payment method var selectedPaymentMethodSystemName = paymentMethodModel.PaymentMethods[0].PaymentMethodSystemName; _genericAttributeService.SaveAttribute<string>(_workContext.CurrentCustomer, SystemCustomerAttributeNames.SelectedPaymentMethod, selectedPaymentMethodSystemName, _storeContext.CurrentStore.Id); var paymentMethodInst = _paymentService.LoadPaymentMethodBySystemName(selectedPaymentMethodSystemName); if (paymentMethodInst == null || !paymentMethodInst.IsPaymentMethodActive(_paymentSettings)) throw new Exception("Selected payment method can't be parsed"); var paymenInfoModel = PreparePaymentInfoModel(paymentMethodInst); return Json(new { update_section = new UpdateSectionJsonModel() { name = "payment-info", html = this.RenderPartialViewToString("OpcPaymentInfo", paymenInfoModel) }, goto_section = "payment_info" }); } else { //customer have to choose a payment method return Json(new { update_section = new UpdateSectionJsonModel() { name = "payment-method", html = this.RenderPartialViewToString("OpcPaymentMethods", paymentMethodModel) }, goto_section = "payment_method" }); } } else { //payment is not required _genericAttributeService.SaveAttribute<string>(_workContext.CurrentCustomer, SystemCustomerAttributeNames.SelectedPaymentMethod, null, _storeContext.CurrentStore.Id); var confirmOrderModel = PrepareConfirmOrderModel(cart); return Json(new { update_section = new UpdateSectionJsonModel() { name = "confirm-order", html = this.RenderPartialViewToString("OpcConfirmOrder", confirmOrderModel) }, goto_section = "confirm_order" }); } } } catch (Exception exc) { _logger.Warning(exc.Message, exc, _workContext.CurrentCustomer); return Json(new { error = 1, message = exc.Message }); } }
public ActionResult OpcSaveBilling(FormCollection form) { try { //validation var cart = _workContext.CurrentCustomer.ShoppingCartItems.Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart).ToList(); if (cart.Count == 0) throw new Exception("Your cart is empty"); if (!_orderSettings.OnePageCheckoutEnabled) throw new Exception("One page checkout is disabled"); if ((_workContext.CurrentCustomer.IsGuest() && !_orderSettings.AnonymousCheckoutAllowed)) throw new Exception("Anonymous checkout is not allowed"); int billingAddressId = 0; int.TryParse(form["billing_address_id"], out billingAddressId); if (billingAddressId > 0) { //existing address var address = _workContext.CurrentCustomer.Addresses.Where(a => a.Id == billingAddressId).FirstOrDefault(); if (address == null) throw new Exception("Address can't be loaded"); _workContext.CurrentCustomer.SetBillingAddress(address); _customerService.UpdateCustomer(_workContext.CurrentCustomer); } else { //new address var model = new CheckoutBillingAddressModel(); TryUpdateModel(model.NewAddress, "BillingNewAddress"); //validate model TryValidateModel(model.NewAddress); if (!ModelState.IsValid) { //model is not valid. redisplay the form with errors var billingAddressModel = PrepareBillingAddressModel(model.NewAddress.CountryId); billingAddressModel.NewAddressPreselected = true; return Json(new { update_section = new UpdateSectionJsonModel() { name = "billing", html = RenderPartialViewToString("OpcBillingAddress", billingAddressModel) } }); } //try to find an address with the same values (don't duplicate records) var address = _workContext.CurrentCustomer.Addresses.ToList().FindAddress( model.NewAddress.FirstName, model.NewAddress.LastName, model.NewAddress.PhoneNumber, model.NewAddress.Email, model.NewAddress.FaxNumber, model.NewAddress.Company, model.NewAddress.Address1, model.NewAddress.Address2, model.NewAddress.City, model.NewAddress.StateProvinceId, model.NewAddress.ZipPostalCode, model.NewAddress.CountryId); if (address == null) { //address is not found. let's create a new one address = model.NewAddress.ToEntity(); address.CreatedOnUtc = DateTime.UtcNow; //some validation if (address.CountryId == 0) address.CountryId = null; if (address.StateProvinceId == 0) address.StateProvinceId = null; _workContext.CurrentCustomer.AddAddress(address); } _workContext.CurrentCustomer.SetBillingAddress(address); _customerService.UpdateCustomer(_workContext.CurrentCustomer); } if (cart.RequiresShipping()) { //shipping is required var shippingAddressModel = PrepareShippingAddressModel(); return Json(new { update_section = new UpdateSectionJsonModel() { name = "shipping", html = RenderPartialViewToString("OpcShippingAddress", shippingAddressModel) }, goto_section = "shipping" }); } else { //shipping is not required _customerService.SaveCustomerAttribute<ShippingOption>(_workContext.CurrentCustomer, SystemCustomerAttributeNames.LastShippingOption, null); var paymentMethodModel = PreparePaymentMethodModel(cart); return Json(new { update_section = new UpdateSectionJsonModel() { name = "payment-method", html = RenderPartialViewToString("OpcPaymentMethods", paymentMethodModel) }, goto_section = "payment_method" }); } } catch (Exception exc) { _logger.Warning(exc.Message, exc, _workContext.CurrentCustomer); return Json(new { error = 1, message = exc.Message }); } }
protected CheckoutBillingAddressModel PrepareBillingAddressModel(int? selectedCountryId = null) { var model = new CheckoutBillingAddressModel(); //existing addresses var addresses = _workContext.CurrentCustomer.Addresses.Where(a => a.Country == null || a.Country.AllowsBilling).ToList(); foreach (var address in addresses) { var addressModel = new AddressModel(); addressModel.PrepareModel(address, false, _addressSettings); model.ExistingAddresses.Add(addressModel); } //new address model.NewAddress.CountryId = selectedCountryId; model.NewAddress.PrepareModel(null, false, _addressSettings, _localizationService, _stateProvinceService, () => _countryService.GetAllCountriesForBilling()); return model; }
public ActionResult NewBillingAddress(CheckoutBillingAddressModel model) { //validation var cart = _workContext.CurrentCustomer.ShoppingCartItems .Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart) .Where(sci => sci.StoreId == _storeContext.CurrentStore.Id) .ToList(); if (cart.Count == 0) return RedirectToRoute("ShoppingCart"); if (UseOnePageCheckout()) return RedirectToRoute("CheckoutOnePage"); if ((_workContext.CurrentCustomer.IsGuest() && !_orderSettings.AnonymousCheckoutAllowed)) return new HttpUnauthorizedResult(); if (ModelState.IsValid) { //try to find an address with the same values (don't duplicate records) var address = _workContext.CurrentCustomer.Addresses.ToList().FindAddress( model.NewAddress.FirstName, model.NewAddress.LastName, model.NewAddress.PhoneNumber, model.NewAddress.Email, model.NewAddress.FaxNumber, model.NewAddress.Company, model.NewAddress.Address1, model.NewAddress.Address2, model.NewAddress.City, model.NewAddress.StateProvinceId, model.NewAddress.ZipPostalCode, model.NewAddress.CountryId); if (address == null) { //address is not found. let's create a new one address = model.NewAddress.ToEntity(); address.CreatedOnUtc = DateTime.UtcNow; //some validation if (address.CountryId == 0) address.CountryId = null; if (address.StateProvinceId == 0) address.StateProvinceId = null; _workContext.CurrentCustomer.Addresses.Add(address); } _workContext.CurrentCustomer.BillingAddress = address; _customerService.UpdateCustomer(_workContext.CurrentCustomer); return RedirectToRoute("CheckoutShippingAddress"); } //If we got this far, something failed, redisplay form model = PrepareBillingAddressModel(selectedCountryId: model.NewAddress.CountryId); return View(model); }
private CheckoutBillingAddressModel PrepareBillingAddressModel(int? selectedCountryId = null) { var model = new CheckoutBillingAddressModel(); //existing addresses var addresses = _workContext.CurrentCustomer.Addresses.Where(a => a.Country == null || a.Country.AllowsBilling).ToList(); foreach (var address in addresses) model.ExistingAddresses.Add(address.ToModel()); //new address model model.NewAddress = new AddressModel(); //countries model.NewAddress.AvailableCountries.Add(new SelectListItem() { Text = _localizationService.GetResource("Address.SelectCountry"), Value = "0" }); foreach (var c in _countryService.GetAllCountriesForBilling()) model.NewAddress.AvailableCountries.Add(new SelectListItem() { Text = c.GetLocalized(x => x.Name), Value = c.Id.ToString(), Selected = (selectedCountryId.HasValue && c.Id == selectedCountryId.Value) }); //states var states = selectedCountryId.HasValue ? _stateProvinceService.GetStateProvincesByCountryId(selectedCountryId.Value).ToList() : new List<StateProvince>(); if (states.Count > 0) { foreach (var s in states) model.NewAddress.AvailableStates.Add(new SelectListItem() { Text = s.GetLocalized(x => x.Name), Value = s.Id.ToString() }); } else model.NewAddress.AvailableStates.Add(new SelectListItem() { Text = _localizationService.GetResource("Address.OtherNonUS"), Value = "0" }); return model; }
public ActionResult OpcSaveBilling(FormCollection form) { try { //validation var cart = _workContext.CurrentCustomer.ShoppingCartItems .Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart) .LimitPerStore(_storeContext.CurrentStore.Id) .ToList(); if (cart.Count == 0) throw new Exception("Your cart is empty"); if (!_orderSettings.OnePageCheckoutEnabled) throw new Exception("One page checkout is disabled"); if ((_workContext.CurrentCustomer.IsGuest() && !_orderSettings.AnonymousCheckoutAllowed)) throw new Exception("Anonymous checkout is not allowed"); int billingAddressId; int.TryParse(form["billing_address_id"], out billingAddressId); if (billingAddressId > 0) { //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; _customerService.UpdateCustomer(_workContext.CurrentCustomer); } else { //new address var model = new CheckoutBillingAddressModel(); TryUpdateModel(model.NewAddress, "BillingNewAddress"); //custom address attributes var customAttributes = form.ParseCustomAddressAttributes(_addressAttributeParser, _addressAttributeService); var customAttributeWarnings = _addressAttributeParser.GetAttributeWarnings(customAttributes); foreach (var error in customAttributeWarnings) { ModelState.AddModelError("", error); } //validate model TryValidateModel(model.NewAddress); if (!ModelState.IsValid) { //model is not valid. redisplay the form with errors var billingAddressModel = PrepareBillingAddressModel(selectedCountryId: model.NewAddress.CountryId); billingAddressModel.NewAddressPreselected = true; return Json(new { update_section = new UpdateSectionJsonModel { name = "billing", html = this.RenderPartialViewToString("OpcBillingAddress", billingAddressModel) }, wrong_billing_address = true, }); } //try to find an address with the same values (don't duplicate records) var address = _workContext.CurrentCustomer.Addresses.ToList().FindAddress( model.NewAddress.FirstName, model.NewAddress.LastName, model.NewAddress.PhoneNumber, model.NewAddress.Email, model.NewAddress.FaxNumber, model.NewAddress.Company, model.NewAddress.Address1, model.NewAddress.Address2, model.NewAddress.City, model.NewAddress.StateProvinceId, model.NewAddress.ZipPostalCode, model.NewAddress.CountryId, customAttributes); if (address == null) { //address is not found. let's create a new one address = model.NewAddress.ToEntity(); address.CustomAttributes = customAttributes; address.CreatedOnUtc = DateTime.UtcNow; //some validation if (address.CountryId == 0) address.CountryId = null; if (address.StateProvinceId == 0) address.StateProvinceId = null; if (address.CountryId.HasValue && address.CountryId.Value > 0) { address.Country = _countryService.GetCountryById(address.CountryId.Value); } _workContext.CurrentCustomer.Addresses.Add(address); } _workContext.CurrentCustomer.BillingAddress = address; _customerService.UpdateCustomer(_workContext.CurrentCustomer); } if (cart.RequiresShipping()) { //shipping is required var shippingAddressModel = PrepareShippingAddressModel(prePopulateNewAddressWithCustomerFields: true); return Json(new { update_section = new UpdateSectionJsonModel { name = "shipping", html = this.RenderPartialViewToString("OpcShippingAddress", shippingAddressModel) }, goto_section = "shipping" }); } //shipping is not required _genericAttributeService.SaveAttribute<ShippingOption>(_workContext.CurrentCustomer, SystemCustomerAttributeNames.SelectedShippingOption, null, _storeContext.CurrentStore.Id); //load next step return OpcLoadStepAfterShippingMethod(cart); } catch (Exception exc) { _logger.Warning(exc.Message, exc, _workContext.CurrentCustomer); return Json(new { error = 1, message = exc.Message }); } }
protected CheckoutBillingAddressModel PrepareBillingAddressModel(int? selectedCountryId = null, bool prePopulateNewAddressWithCustomerFields = false) { var model = new CheckoutBillingAddressModel(); //existing addresses var addresses = _workContext.CurrentCustomer.Addresses //allow billing .Where(a => a.Country == null || a.Country.AllowsBilling) //enabled for the current store .Where(a => a.Country == null || _storeMappingService.Authorize(a.Country)) .ToList(); foreach (var address in addresses) { var addressModel = new AddressModel(); addressModel.PrepareModel(address, false, _addressSettings); model.ExistingAddresses.Add(addressModel); } //new address model.NewAddress.CountryId = selectedCountryId; model.NewAddress.PrepareModel(null, false, _addressSettings, _localizationService, _stateProvinceService, () => _countryService.GetAllCountriesForBilling(), prePopulateNewAddressWithCustomerFields, _workContext.CurrentCustomer); return model; }
protected virtual CheckoutBillingAddressModel PrepareBillingAddressModel(int? selectedCountryId = null, bool prePopulateNewAddressWithCustomerFields = false) { var model = new CheckoutBillingAddressModel(); //existing addresses var addresses = _workContext.CurrentCustomer.Addresses //allow billing .Where(a => a.Country == null || a.Country.AllowsBilling) //enabled for the current store .Where(a => a.Country == null || _storeMappingService.Authorize(a.Country)) .ToList(); foreach (var address in addresses) { var addressModel = new AddressModel(); addressModel.PrepareModel( address: address, excludeProperties: false, addressSettings: _addressSettings, addressAttributeFormatter: _addressAttributeFormatter); model.ExistingAddresses.Add(addressModel); } //new address model.NewAddress.CountryId = selectedCountryId; model.NewAddress.PrepareModel(address: null, excludeProperties: false, addressSettings: _addressSettings, localizationService: _localizationService, stateProvinceService: _stateProvinceService, addressAttributeService: _addressAttributeService, addressAttributeParser: _addressAttributeParser, loadCountries: () => _countryService.GetAllCountriesForBilling(), prePopulateWithCustomerFields: prePopulateNewAddressWithCustomerFields, customer: _workContext.CurrentCustomer); return model; }
public ActionResult NewBillingAddress(CheckoutBillingAddressModel model, FormCollection form) { //validation var cart = _workContext.CurrentCustomer.ShoppingCartItems .Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart) .LimitPerStore(_storeContext.CurrentStore.Id) .ToList(); if (!cart.Any()) return RedirectToRoute("ShoppingCart"); if (_orderSettings.OnePageCheckoutEnabled) return RedirectToRoute("CheckoutOnePage"); if (_workContext.CurrentCustomer.IsGuest() && !_orderSettings.AnonymousCheckoutAllowed) return new HttpUnauthorizedResult(); //custom address attributes var customAttributes = form.ParseCustomAddressAttributes(_addressAttributeParser, _addressAttributeService); var customAttributeWarnings = _addressAttributeParser.GetAttributeWarnings(customAttributes); foreach (var error in customAttributeWarnings) { ModelState.AddModelError("", error); } if (ModelState.IsValid) { //try to find an address with the same values (don't duplicate records) var address = _workContext.CurrentCustomer.Addresses.ToList().FindAddress( model.NewAddress.FirstName, model.NewAddress.LastName, model.NewAddress.PhoneNumber, model.NewAddress.Email, model.NewAddress.FaxNumber, model.NewAddress.Company, model.NewAddress.Address1, model.NewAddress.Address2, model.NewAddress.City, model.NewAddress.StateProvinceId, model.NewAddress.ZipPostalCode, model.NewAddress.CountryId, customAttributes); if (address == null) { //address is not found. let's create a new one address = model.NewAddress.ToEntity(); address.CustomAttributes = customAttributes; address.CreatedOnUtc = DateTime.UtcNow; //some validation if (address.CountryId == 0) address.CountryId = null; if (address.StateProvinceId == 0) address.StateProvinceId = null; _workContext.CurrentCustomer.Addresses.Add(address); } _workContext.CurrentCustomer.BillingAddress = address; _customerService.UpdateCustomer(_workContext.CurrentCustomer); //ship to the same address? if (_shippingSettings.ShipToSameAddress && model.ShipToSameAddress && cart.RequiresShipping()) { _workContext.CurrentCustomer.ShippingAddress = _workContext.CurrentCustomer.BillingAddress; _customerService.UpdateCustomer(_workContext.CurrentCustomer); //reset selected shipping method (in case if "pick up in store" was selected) _genericAttributeService.SaveAttribute<ShippingOption>(_workContext.CurrentCustomer, SystemCustomerAttributeNames.SelectedShippingOption, null, _storeContext.CurrentStore.Id); _genericAttributeService.SaveAttribute<PickupPoint>(_workContext.CurrentCustomer, SystemCustomerAttributeNames.SelectedPickupPoint, null, _storeContext.CurrentStore.Id); //limitation - "Ship to the same address" doesn't properly work in "pick up in store only" case (when no shipping plugins are available) return RedirectToRoute("CheckoutShippingMethod"); } return RedirectToRoute("CheckoutShippingAddress"); } //If we got this far, something failed, redisplay form model = PrepareBillingAddressModel(cart, selectedCountryId: model.NewAddress.CountryId, overrideAttributesXml: customAttributes); return View(model); }
protected virtual CheckoutBillingAddressModel PrepareBillingAddressModel(IList<ShoppingCartItem> cart, int? selectedCountryId = null, bool prePopulateNewAddressWithCustomerFields = false, string overrideAttributesXml = "") { var model = new CheckoutBillingAddressModel(); model.ShipToSameAddressAllowed = _shippingSettings.ShipToSameAddress && cart.RequiresShipping(); model.ShipToSameAddress = true; //existing addresses var addresses = _workContext.CurrentCustomer.Addresses .Where(a => a.Country == null || (//published a.Country.Published && //allow billing a.Country.AllowsBilling && //enabled for the current store _storeMappingService.Authorize(a.Country))) .ToList(); foreach (var address in addresses) { var addressModel = new AddressModel(); addressModel.PrepareModel( address: address, excludeProperties: false, addressSettings: _addressSettings, addressAttributeFormatter: _addressAttributeFormatter); model.ExistingAddresses.Add(addressModel); } //new address model.NewAddress.CountryId = selectedCountryId; model.NewAddress.PrepareModel(address: null, excludeProperties: false, addressSettings: _addressSettings, localizationService: _localizationService, stateProvinceService: _stateProvinceService, addressAttributeService: _addressAttributeService, addressAttributeParser: _addressAttributeParser, loadCountries: () => _countryService.GetAllCountriesForBilling(_workContext.WorkingLanguage.Id), prePopulateWithCustomerFields: prePopulateNewAddressWithCustomerFields, customer: _workContext.CurrentCustomer, overrideAttributesXml: overrideAttributesXml); return model; }
public ActionResult NewBillingAddress(CheckoutBillingAddressModel model, FormCollection form) { //validation //var cart = _workContext.CurrentCustomer.ShoppingCartItems // .Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart) // .LimitPerStore(_storeContext.CurrentStore.Id) // .ToList(); var gcCount = _workContext.CurrentCustomer.ShoppingCartItems .Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart && sci.Product.AdminComment == "Gift Card") .LimitPerStore(_storeContext.CurrentStore.Id) .Count(); IList<ShoppingCartItem> cart = new List<ShoppingCartItem>(); if (gcCount != 0) { cart = _workContext.CurrentCustomer.ShoppingCartItems .Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart && sci.Product.AdminComment == "Gift Card") .LimitPerStore(_storeContext.CurrentStore.Id) .ToList(); } else { cart = _workContext.CurrentCustomer.ShoppingCartItems .Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart) .LimitPerStore(_storeContext.CurrentStore.Id) .ToList(); } if (cart.Count == 0) return RedirectToRoute("ShoppingCart"); if (_orderSettings.OnePageCheckoutEnabled) return RedirectToRoute("CheckoutOnePage"); if ((_workContext.CurrentCustomer.IsGuest() && !_orderSettings.AnonymousCheckoutAllowed)) return new HttpUnauthorizedResult(); //custom address attributes var customAttributes = form.ParseCustomAddressAttributes(_addressAttributeParser, _addressAttributeService); var customAttributeWarnings = _addressAttributeParser.GetAttributeWarnings(customAttributes); foreach (var error in customAttributeWarnings) { ModelState.AddModelError("", error); } if (ModelState.IsValid) { //try to find an address with the same values (don't duplicate records) var address = _workContext.CurrentCustomer.Addresses.ToList().FindAddress( model.NewAddress.FirstName, model.NewAddress.LastName, model.NewAddress.PhoneNumber, model.NewAddress.Email, model.NewAddress.FaxNumber, model.NewAddress.Company, model.NewAddress.Address1, model.NewAddress.Address2, model.NewAddress.City, model.NewAddress.StateProvinceId, model.NewAddress.ZipPostalCode, model.NewAddress.CountryId, customAttributes); if (address == null) { //address is not found. let's create a new one address = model.NewAddress.ToEntity(); address.CustomAttributes = customAttributes; address.CreatedOnUtc = DateTime.UtcNow; //some validation if (address.CountryId == 0) address.CountryId = null; if (address.StateProvinceId == 0) address.StateProvinceId = null; _workContext.CurrentCustomer.Addresses.Add(address); } _workContext.CurrentCustomer.BillingAddress = address; _customerService.UpdateCustomer(_workContext.CurrentCustomer); return RedirectToRoute("CheckoutShippingAddress"); } //If we got this far, something failed, redisplay form model = PrepareBillingAddressModel(selectedCountryId: model.NewAddress.CountryId); return View(model); }
public ActionResult NewBillingAddress(CheckoutBillingAddressModel model, FormCollection form) { bool errors = false; string errorMessage = ""; //validation var cart = _workContext.CurrentCustomer.ShoppingCartItems .Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart) .LimitPerStore(_storeContext.CurrentStore.Id) .ToList(); if (cart.Count == 0) return RedirectToRoute("ShoppingCart"); if (_orderSettings.OnePageCheckoutEnabled) return RedirectToRoute("CheckoutOnePage"); if ((_workContext.CurrentCustomer.IsGuest() && !_orderSettings.AnonymousCheckoutAllowed)) return new HttpUnauthorizedResult(); //custom address attributes var customAttributes = form.ParseCustomAddressAttributes(_addressAttributeParser, _addressAttributeService); var customAttributeWarnings = _addressAttributeParser.GetAttributeWarnings(customAttributes); foreach (var error in customAttributeWarnings) { ModelState.AddModelError("", error); } if (ModelState.IsValid) { if (model.NewAddress.Id > 0) { var address = _workContext.CurrentCustomer.Addresses.FirstOrDefault(a => a.Id == model.NewAddress.Id); if (address != null) { //nyusoft var products = _workContext.CurrentCustomer.ShoppingCartItems.Where(x => x.ShoppingCartType == ShoppingCartType.ShoppingCart).ToList(); if (products != null) { //foreach (var p in products) { for (int i = 0; i < products.Count; i++) { var p = products[i]; if (p != null) { var city = _zipcodeService.GetZipcodeByName(model.NewAddress.ZipPostalCode.Trim()); if (city != null) { ProductCities productcity = null; foreach (var item in city) { productcity = _productCitiesService.GetProductsCitiesByProductId(p.ProductId).Where(x => x.CityID == item.CityID).FirstOrDefault(); if (productcity != null) break; } if (productcity == null) { errors = true; string msg = p.Product.Name + " is not available on your zip code."; errorMessage = msg; TempData["message"] = errorMessage; //return RedirectToRoute("CheckoutBillingAddress",model); model = PrepareBillingAddressModel(selectedCountryId: model.NewAddress.CountryId); return View(model); } } else { errors = true; string msg = "Our services are not available on your zip code."; errorMessage = msg; TempData["message"] = errorMessage; model = PrepareBillingAddressModel(selectedCountryId: model.NewAddress.CountryId); return View(model); //return RedirectToRoute("CheckoutBillingAddress"); } } } } if (errors == false) { address = model.NewAddress.ToEntity(address); address.CustomAttributes = customAttributes; _addressService.UpdateAddress(address); _workContext.CurrentCustomer.BillingAddress = address; _workContext.CurrentCustomer.ShippingAddress = address; _customerService.UpdateCustomer(_workContext.CurrentCustomer); //return RedirectToRoute("CheckoutShippingAddress"); return RedirectToRoute("CheckoutConfirmAddress"); } else { //return RedirectToRoute("CheckoutBillingAddress"); model = PrepareBillingAddressModel(selectedCountryId: model.NewAddress.CountryId); return View(model); } } } else { //try to find an address with the same values (don't duplicate records) var address = _workContext.CurrentCustomer.Addresses.ToList().FindAddress( model.NewAddress.FirstName, model.NewAddress.LastName, model.NewAddress.PhoneNumber, model.NewAddress.Email, model.NewAddress.FaxNumber, model.NewAddress.Company, model.NewAddress.Address1, model.NewAddress.Address2, model.NewAddress.City, model.NewAddress.StateProvinceId, model.NewAddress.ZipPostalCode, model.NewAddress.CountryId, customAttributes); if (address == null) { var products = _workContext.CurrentCustomer.ShoppingCartItems.Where(x => x.ShoppingCartType == ShoppingCartType.ShoppingCart).ToList(); if (products != null) { //foreach (var p in products) { for (int i = 0; i < products.Count; i++) { var p = products[i]; if (p != null) { var city = _zipcodeService.GetZipcodeByName(model.NewAddress.ZipPostalCode.Trim()); if (city != null) { ProductCities productcity = null; foreach (var item in city) { productcity = _productCitiesService.GetProductsCitiesByProductId(p.ProductId).Where(x => x.CityID == item.CityID).FirstOrDefault(); if (productcity != null) break; } if (productcity == null) { errors = true; string msg = p.Product.Name + " is not available on your zip code."; errorMessage = msg; TempData["message"] = errorMessage; //return RedirectToRoute("CheckoutBillingAddress"); model = PrepareBillingAddressModel(selectedCountryId: model.NewAddress.CountryId); return View(model); } } else { errors = true; string msg = "Our services are not available on your zip code."; errorMessage = msg; TempData["message"] = errorMessage; //return RedirectToRoute("CheckoutBillingAddress"); model = PrepareBillingAddressModel(selectedCountryId: model.NewAddress.CountryId); return View(model); } } } } if (errors == false) { //address is not found. let's create a new one address = model.NewAddress.ToEntity(); address.CustomAttributes = customAttributes; address.CreatedOnUtc = DateTime.UtcNow; //some validation if (address.CountryId == 0) address.CountryId = null; if (address.StateProvinceId == 0) address.StateProvinceId = null; _workContext.CurrentCustomer.Addresses.Add(address); _workContext.CurrentCustomer.BillingAddress = address; _workContext.CurrentCustomer.ShippingAddress = address; _customerService.UpdateCustomer(_workContext.CurrentCustomer); return RedirectToRoute("CheckoutConfirmAddress"); } } else { var products = _workContext.CurrentCustomer.ShoppingCartItems.Where(x => x.ShoppingCartType == ShoppingCartType.ShoppingCart).ToList(); if (products != null) { //foreach (var p in products) { for (int i = 0; i < products.Count; i++) { var p = products[i]; if (p != null) { var city = _zipcodeService.GetZipcodeByName(model.NewAddress.ZipPostalCode.Trim()); if (city != null) { ProductCities productcity = null; foreach (var item in city) { productcity = _productCitiesService.GetProductsCitiesByProductId(p.ProductId).Where(x => x.CityID == item.CityID).FirstOrDefault(); if (productcity != null) break; } if (productcity == null) { errors = true; string msg = p.Product.Name + " is not available on your zip code."; errorMessage = msg; TempData["message"] = errorMessage; //return RedirectToRoute("CheckoutBillingAddress"); model = PrepareBillingAddressModel(selectedCountryId: model.NewAddress.CountryId); return View(model); } } else { errors = true; string msg = "Our services are not available on your zip code."; errorMessage = msg; TempData["message"] = errorMessage; //return RedirectToRoute("CheckoutBillingAddress"); model = PrepareBillingAddressModel(selectedCountryId: model.NewAddress.CountryId); return View(model); } } } } if (errors == false) { _workContext.CurrentCustomer.BillingAddress = address; _customerService.UpdateCustomer(_workContext.CurrentCustomer); //return RedirectToRoute("CheckoutShippingAddress"); return RedirectToRoute("CheckoutConfirmAddress"); } } } } //If we got this far, something failed, redisplay form model = PrepareBillingAddressModel(selectedCountryId: model.NewAddress.CountryId); return View(model); }