I used to have a validation for Shipping Address though in OPC: If ShippinAddress was Hawaii, Alaska or Puerto Rico, throw an exception message.
public ActionResult OpcSaveShipping(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 (!UseOnePageCheckout())
throw new Exception("One page checkout is disabled");
if ((_workContext.CurrentCustomer.IsGuest() && !_orderSettings.AnonymousCheckoutAllowed))
throw new Exception("Anonymous checkout is not allowed");
if (!cart.RequiresShipping())
throw new Exception("Shipping is not required");
int shippingAddressId = 0;
int.TryParse(form["shipping_address_id"], out shippingAddressId);
if (shippingAddressId > 0)
{
//existing address
var address = _workContext.CurrentCustomer.Addresses.Where(a => a.Id == shippingAddressId).FirstOrDefault();
if (address == null)
throw new Exception("Address can't be loaded");
if (address.StateProvinceId == 4 || address.StateProvinceId == 18 || address.StateProvinceId == 49)
throw new Exception("We cannot ship ground to Alaska, Hawaii or Puerto Rico.");
_workContext.CurrentCustomer.SetShippingAddress(address);
_customerService.UpdateCustomer(_workContext.CurrentCustomer);
}
else
{
//new address
var model = new CheckoutShippingAddressModel();
TryUpdateModel(model.NewAddress, "ShippingNewAddress");
//validate model
TryValidateModel(model.NewAddress);
if (!ModelState.IsValid)
{
//model is not valid. redisplay the form with errors
var shippingAddressModel = PrepareShippingAddressModel(model.NewAddress.CountryId);
shippingAddressModel.NewAddressPreselected = true;
return Json(new
{
update_section = new UpdateSectionJsonModel()
{
name = "shipping",
html = RenderPartialViewToString("OpcShippingAddress", shippingAddressModel)
}
});
}
//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 = model.NewAddress.ToEntity();
address.CreatedOnUtc = DateTime.UtcNow;
//some validation
if (address.StateProvinceId == 4 || address.StateProvinceId == 18 || address.StateProvinceId == 49)
throw new Exception("We cannot ship ground to Alaska, Hawaii or Puerto Rico.");
if (address.CountryId == 0)
address.CountryId = null;
if (address.StateProvinceId == 0)
address.StateProvinceId = null;
_workContext.CurrentCustomer.AddAddress(address);
}
_workContext.CurrentCustomer.SetShippingAddress(address);
_customerService.UpdateCustomer(_workContext.CurrentCustomer);
}
var shippingMethodModel = PrepareShippingMethodModel(cart);
return Json(new
{
update_section = new UpdateSectionJsonModel()
{
name = "shipping-method",
html = RenderPartialViewToString("OpcShippingMethods", shippingMethodModel)
},
goto_section = "shipping_method"
});
}
catch (Exception exc)
{
_logger.Warning(exc.Message, exc, _workContext.CurrentCustomer);
return Json(new { error = 1, message = exc.Message });
}
}
I tried using a catch statement for multi-step checkout too, but it doesn't work. How can I try to validate the Shipping Address so that it can display an error if Existing or New Shipping Address has Hawaii, Alaska or Puerto Rico?
When I tried the exact same coding for Multi-Step checkout, I kept getting an error stating the request was being blocked due to sensitive info and that I would need to set JsonResultBehavior to AllowGet.
Any help appreciated! Thanks.