Skip to content

Commit

Permalink
nopSolutions#4530 display error messages in Estimate shipping window
Browse files Browse the repository at this point in the history
  • Loading branch information
Marianna Koroleva authored and Marianna Koroleva committed Apr 30, 2020
1 parent 73b6a71 commit 9809613
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17794,7 +17794,7 @@
<Value>Estimated Delivery</Value>
</LocaleResource>
<LocaleResource Name="Shipping.EstimateShippingPopUp.ShippingOption.IsNotFound">
<Value>Selected shipping option couldn't be found</Value>
<Value>Selected shipping option is not found</Value>
</LocaleResource>
<LocaleResource Name="Shipping.EstimateShippingPopUp.ShippingOption.Name">
<Value>Name</Value>
Expand Down
32 changes: 22 additions & 10 deletions src/Presentation/Nop.Web/Controllers/ProductController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,22 +200,31 @@ public virtual IActionResult ProductDetails(int productId, int updatecartitemid
public virtual IActionResult EstimateShipping([FromQuery] ProductEstimateShippingModel model, IFormCollection form)
{
if (model == null)
return BadRequest();

var product = _productService.GetProductById(model.ProductId);
if (product == null || product.Deleted || !product.Published)
return NotFound();
model = new ProductEstimateShippingModel();

var errors = new List<string>();

if (string.IsNullOrEmpty(model.ZipPostalCode))
errors.Add(_localizationService.GetResource("Shipping.EstimateShipping.ZipPostalCode.Required"));

if (model.CountryId == null || model.CountryId == 0)
errors.Add(_localizationService.GetResource("Shipping.EstimateShipping.Country.Required"));

if (errors.Count > 0)
return BadRequest(new { Errors = errors });
return Json(new {
success = false,
errors = errors
});

var product = _productService.GetProductById(model.ProductId);
if (product == null || product.Deleted || !product.Published)
{
errors.Add(_localizationService.GetResource("Shipping.EstimateShippingPopUp.ShippingOption.IsNotFound"));
return Json(new
{
success = false,
errors = errors
});
}

var wrappedProduct = new ShoppingCartItem()
{
Expand All @@ -227,7 +236,6 @@ public virtual IActionResult EstimateShipping([FromQuery] ProductEstimateShippin
};

var addToCartWarnings = new List<string>();

//customer entered price
wrappedProduct.CustomerEnteredPrice = _productAttributeParser.ParseCustomerEnteredPrice(product, form);

Expand All @@ -242,9 +250,13 @@ public virtual IActionResult EstimateShipping([FromQuery] ProductEstimateShippin
wrappedProduct.RentalStartDateUtc = rentalStartDate;
wrappedProduct.RentalEndDateUtc = rentalEndDate;

var modelResult = _shoppingCartModelFactory.PrepareEstimateShippingResultModel(new [] { wrappedProduct }, model.CountryId, model.StateProvinceId, model.ZipPostalCode, false);
var result = _shoppingCartModelFactory.PrepareEstimateShippingResultModel(new [] { wrappedProduct }, model.CountryId, model.StateProvinceId, model.ZipPostalCode, false);

return Json(modelResult);
return Json(new
{
success = true,
result = result
});
}

#endregion
Expand Down
45 changes: 27 additions & 18 deletions src/Presentation/Nop.Web/Controllers/ShoppingCartController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -414,21 +414,22 @@ protected virtual IActionResult GetProductToCartDetails(List<string> addToCartWa
public virtual IActionResult SelectShippingOption([FromQuery]string name, [FromQuery]EstimateShippingModel model, IFormCollection form)
{
if (model == null)
return BadRequest();
model = new EstimateShippingModel();

var errors = new List<string>();

if (string.IsNullOrEmpty(model.ZipPostalCode))
errors.Add(_localizationService.GetResource("Shipping.EstimateShipping.ZipPostalCode.Required"));

if (model.CountryId == null || model.CountryId == 0)
errors.Add(_localizationService.GetResource("Shipping.EstimateShipping.Country.Required"));

if (errors.Count > 0)
return BadRequest(new { Errors = errors });
return Json(new {
success = false,
errors = errors
});

var cart = _shoppingCartService.GetShoppingCart(_workContext.CurrentCustomer, ShoppingCartType.ShoppingCart, _storeContext.CurrentStore.Id);

//parse and save checkout attributes
ParseAndSaveCheckoutAttributes(cart, form);

Expand Down Expand Up @@ -463,13 +464,15 @@ public virtual IActionResult SelectShippingOption([FromQuery]string name, [FromQ
}
}

if (errors.Count > 0)
return BadRequest(new { Errors = errors });

selectedShippingOption = shippingOptions.Find(so => !string.IsNullOrEmpty(so.Name) && so.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase));

if (selectedShippingOption == null)
return BadRequest(new { Errors = new List<string>() { _localizationService.GetResource("Shipping.EstimateShippingPopUp.ShippingOption.IsNotFound") } });
errors.Add(_localizationService.GetResource("Shipping.EstimateShippingPopUp.ShippingOption.IsNotFound"));

if (errors.Count > 0)
return Json(new {
success = false,
errors = errors
});

//reset pickup point
_genericAttributeService.SaveAttribute<PickupPoint>(_workContext.CurrentCustomer,
Expand All @@ -483,6 +486,7 @@ public virtual IActionResult SelectShippingOption([FromQuery]string name, [FromQ

return Json(new
{
success = true,
ordertotalssectionhtml
});
}
Expand Down Expand Up @@ -1371,27 +1375,32 @@ public virtual IActionResult ApplyGiftCard(string giftcardcouponcode, IFormColle
public virtual IActionResult GetEstimateShipping(EstimateShippingModel model, IFormCollection form)
{
if (model == null)
return BadRequest();

var cart = _shoppingCartService.GetShoppingCart(_workContext.CurrentCustomer, ShoppingCartType.ShoppingCart, _storeContext.CurrentStore.Id);

//parse and save checkout attributes
ParseAndSaveCheckoutAttributes(cart, form);
model = new EstimateShippingModel();

var errors = new List<string>();

if (string.IsNullOrEmpty(model.ZipPostalCode))
errors.Add(_localizationService.GetResource("Shipping.EstimateShipping.ZipPostalCode.Required"));

if (model.CountryId == null || model.CountryId == 0)
errors.Add(_localizationService.GetResource("Shipping.EstimateShipping.Country.Required"));

if (errors.Count > 0)
return BadRequest(new { Errors = errors });
return Json(new {
success = false,
errors = errors
});

var cart = _shoppingCartService.GetShoppingCart(_workContext.CurrentCustomer, ShoppingCartType.ShoppingCart, _storeContext.CurrentStore.Id);
//parse and save checkout attributes
ParseAndSaveCheckoutAttributes(cart, form);

var result = _shoppingCartModelFactory.PrepareEstimateShippingResultModel(cart, model.CountryId, model.StateProvinceId, model.ZipPostalCode, true);

return Json(result);
return Json(new
{
success = true,
result = result
});
}

[HttpPost, ActionName("Cart")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
let initialized = false;
let localizedData = {
NoShippingOptions: '@T("Shipping.EstimateShippingPopUp.NoShippingOptions")'
NoShippingOptions: '@T("Shipping.EstimateShippingPopUp.NoShippingOptions")',
countryErrorMessage: '@T("Shipping.EstimateShipping.Country.Required")',
zipPostalCodeErrorMessage: '@T("Shipping.EstimateShipping.ZipPostalCode.Required")',
};
let urlFactory = function (address) {
let params = $.param({
Expand Down Expand Up @@ -56,7 +58,7 @@
popUp.selectShippingOption();
},
selectedOption: function (option) {
if (option && option.provider && option.price && popUp.addressIsValid(option.address)) {
if (option && option.provider && option.price && popUp.validateAddress(option.address)) {
let shippingContent = $('#open-estimate-shipping-popup');
let shippingTitle = $('<div/>').addClass('shipping-title')
Expand All @@ -78,13 +80,14 @@
.html($('<span/>').text('@T("Products.EstimateShipping.NoSelectedShippingOption")'))
.append($('<i/>').addClass('arrow-down'));
}
popUp.closePopup();
}
};
popUp.init('#product-details-form', urlFactory, handlers, localizedData);
let initialLoad = function () {
let address = popUp.getShippingAddress();
if (popUp.addressIsValid(address))
if (popUp.validateAddress(address))
popUp.getShippingOptions(address);
else
popUp.selectShippingOption();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
let popUp = EstimateShippingPopUp;
let localizedData = {
NoShippingOptions: '@T("Shipping.EstimateShippingPopUp.NoShippingOptions")'
NoShippingOptions: '@T("Shipping.EstimateShippingPopUp.NoShippingOptions")',
countryErrorMessage: '@T("Shipping.EstimateShipping.Country.Required")',
zipPostalCodeErrorMessage: '@T("Shipping.EstimateShipping.ZipPostalCode.Required")',
};
let urlFactory = function (address) {
let params = $.param({
Expand All @@ -28,7 +30,7 @@
let handlers = {
selectedOption: function (option) {
let params;
if (option && option.provider && option.price && popUp.addressIsValid(option.address)) {
if (option && option.provider && option.price && popUp.validateAddress(option.address)) {
params = $.param({
Name: option.provider,
CountryId: option.address.countryId,
Expand All @@ -46,8 +48,15 @@
displayAjaxLoading(true);
},
success: function (response) {
if (response.ordertotalssectionhtml)
$('.total-info').replaceWith(response.ordertotalssectionhtml);
popUp.clearErrorMessage();
if (response.success) {
if (response.ordertotalssectionhtml) {
$('.total-info').replaceWith(response.ordertotalssectionhtml);
}
popUp.closePopup();
} else {
popUp.showErrorMessage(response.errors);
}
},
complete: function () {
displayAjaxLoading(false);
Expand All @@ -58,7 +67,7 @@
popUp.init('#shopping-cart-form', urlFactory, handlers, localizedData);
let address = popUp.getShippingAddress();
if (popUp.addressIsValid(address))
if (popUp.validateAddress(address))
popUp.getShippingOptions(address);
else
popUp.selectShippingOption();
Expand Down
Loading

0 comments on commit 9809613

Please sign in to comment.