public ActionResult GetEstimateShipping(EstimateShippingModel shippingModel, FormCollection form) { var cart = _workContext.CurrentCustomer.ShoppingCartItems .Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart) .LimitPerStore(_storeContext.CurrentStore.Id) .ToList(); //parse and save checkout attributes ParseAndSaveCheckoutAttributes(cart, form); var model = new ShoppingCartModel(); model.EstimateShipping.CountryId = shippingModel.CountryId; model.EstimateShipping.StateProvinceId = shippingModel.StateProvinceId; model.EstimateShipping.ZipPostalCode = shippingModel.ZipPostalCode; PrepareShoppingCartModel(model, cart,setEstimateShippingDefaultAddress: false); if (cart.RequiresShipping()) { var address = new Address() { CountryId = shippingModel.CountryId, Country = shippingModel.CountryId.HasValue ? _countryService.GetCountryById(shippingModel.CountryId.Value) : null, StateProvinceId = shippingModel.StateProvinceId, StateProvince = shippingModel.StateProvinceId.HasValue ? _stateProvinceService.GetStateProvinceById(shippingModel.StateProvinceId.Value) : null, ZipPostalCode = shippingModel.ZipPostalCode, }; GetShippingOptionResponse getShippingOptionResponse = _shippingService .GetShippingOptions(cart, address, "", _storeContext.CurrentStore.Id); if (!getShippingOptionResponse.Success) { foreach (var error in getShippingOptionResponse.Errors) model.EstimateShipping.Warnings.Add(error); } else { if (getShippingOptionResponse.ShippingOptions.Count > 0) { foreach (var shippingOption in getShippingOptionResponse.ShippingOptions) { var soModel = new EstimateShippingModel.ShippingOptionModel() { Name = shippingOption.Name, Description = shippingOption.Description, }; //calculate discounted and taxed rate Discount appliedDiscount = null; decimal shippingTotal = _orderTotalCalculationService.AdjustShippingRate(shippingOption.Rate, cart, out appliedDiscount); decimal rateBase = _taxService.GetShippingPrice(shippingTotal, _workContext.CurrentCustomer); decimal rate = _currencyService.ConvertFromPrimaryStoreCurrency(rateBase, _workContext.WorkingCurrency); soModel.Price = _priceFormatter.FormatShippingPrice(rate, true); model.EstimateShipping.ShippingOptions.Add(soModel); } //pickup in store? if (_shippingSettings.AllowPickUpInStore) { var soModel = new EstimateShippingModel.ShippingOptionModel() { Name = _localizationService.GetResource("Checkout.PickUpInStore"), Description = _localizationService.GetResource("Checkout.PickUpInStore.Description"), Price = _priceFormatter.FormatShippingPrice(decimal.Zero, true) }; model.EstimateShipping.ShippingOptions.Add(soModel); } } else { model.EstimateShipping.Warnings.Add(_localizationService.GetResource("Checkout.ShippingIsNotAllowed")); } } } return View(model); }
public ActionResult GetEstimateShipping(EstimateShippingModel shippingModel) { var cart = _workContext.CurrentCustomer.ShoppingCartItems.Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart).ToList(); var model = new ShoppingCartModel(); model.EstimateShipping.CountryId = shippingModel.CountryId; model.EstimateShipping.StateProvinceId = shippingModel.StateProvinceId; model.EstimateShipping.ZipPostalCode = shippingModel.ZipPostalCode; model = PrepareShoppingCartModel(model, cart, true, false); if (cart.RequiresShipping()) { var address = new Address() { CountryId = shippingModel.CountryId, Country = shippingModel.CountryId.HasValue ? _countryService.GetCountryById(shippingModel.CountryId.Value) : null, StateProvinceId = shippingModel.StateProvinceId, StateProvince = shippingModel.StateProvinceId.HasValue ? _stateProvinceService.GetStateProvinceById(shippingModel.StateProvinceId.Value) : null, ZipPostalCode = shippingModel.ZipPostalCode, }; GetShippingOptionResponse getShippingOptionResponse = _shippingService.GetShippingOptions(cart, address); if (!getShippingOptionResponse.Success) { foreach (var error in getShippingOptionResponse.Errors) model.EstimateShipping.Warnings.Add(error); } else { if (getShippingOptionResponse.ShippingOptions.Count > 0) { foreach (var shippingOption in getShippingOptionResponse.ShippingOptions) { var soModel = new EstimateShippingModel.ShippingOptionModel() { Name = shippingOption.Name, Description = shippingOption.Description, }; //calculate discounted and taxed rate Discount appliedDiscount = null; decimal shippingTotalWithoutDiscount = shippingOption.Rate; decimal discountAmount = _orderTotalCalculationService.GetShippingDiscount(_workContext.CurrentCustomer, shippingTotalWithoutDiscount, out appliedDiscount); decimal shippingTotalWithDiscount = shippingTotalWithoutDiscount - discountAmount; if (shippingTotalWithDiscount < decimal.Zero) shippingTotalWithDiscount = decimal.Zero; shippingTotalWithDiscount = Math.Round(shippingTotalWithDiscount, 2); decimal rateBase = _taxService.GetShippingPrice(shippingTotalWithDiscount, _workContext.CurrentCustomer); decimal rate = _currencyService.ConvertFromPrimaryStoreCurrency(rateBase, _workContext.WorkingCurrency); soModel.Price = _priceFormatter.FormatShippingPrice(rate, true); model.EstimateShipping.ShippingOptions.Add(soModel); } } else { model.EstimateShipping.Warnings.Add(_localizationService.GetResource("Checkout.ShippingIsNotAllowed")); } } } return View(model); }