private async Task PreparePickupPoints(CheckoutShippingAddressModel model, GetShippingAddress request) { var pickupPoints = await _shippingService.LoadActivePickupPoints(request.Store.Id); if (pickupPoints.Any()) { foreach (var pickupPoint in pickupPoints) { var pickupPointModel = new CheckoutPickupPointModel() { Id = pickupPoint.Id, Name = pickupPoint.Name, Description = pickupPoint.Description, Address = pickupPoint.Address, }; if (pickupPoint.PickupFee > 0) { var amount = (await _taxService.GetShippingPrice(pickupPoint.PickupFee, request.Customer)).shippingPrice; amount = await _currencyService.ConvertFromPrimaryStoreCurrency(amount, request.Currency); pickupPointModel.PickupFee = _priceFormatter.FormatShippingPrice(amount, true); } model.PickupPoints.Add(pickupPointModel); } } if (!(await _shippingService.LoadActiveShippingRateComputationMethods(request.Store.Id)).Any()) { if (!pickupPoints.Any()) { model.Warnings.Add(_localizationService.GetResource("Checkout.ShippingIsNotAllowed")); model.Warnings.Add(_localizationService.GetResource("Checkout.PickupPoints.NotAvailable")); } model.PickUpInStoreOnly = true; model.PickUpInStore = true; } }
public virtual async Task <CheckoutShippingAddressModel> PrepareShippingAddress(string selectedCountryId = null, bool prePopulateNewAddressWithCustomerFields = false, string overrideAttributesXml = "") { var model = new CheckoutShippingAddressModel(); //allow pickup in store? model.AllowPickUpInStore = _shippingSettings.AllowPickUpInStore; if (model.AllowPickUpInStore) { var pickupPoints = await _shippingService.LoadActivePickupPoints(_storeContext.CurrentStore.Id); if (pickupPoints.Any()) { foreach (var pickupPoint in pickupPoints) { var pickupPointModel = new CheckoutPickupPointModel() { Id = pickupPoint.Id, Name = pickupPoint.Name, Description = pickupPoint.Description, Address = pickupPoint.Address, }; if (pickupPoint.PickupFee > 0) { var amount = (await _taxService.GetShippingPrice(pickupPoint.PickupFee, _workContext.CurrentCustomer)).shippingPrice; amount = await _currencyService.ConvertFromPrimaryStoreCurrency(amount, _workContext.WorkingCurrency); pickupPointModel.PickupFee = _priceFormatter.FormatShippingPrice(amount, true); } model.PickupPoints.Add(pickupPointModel); } } if (!(await _shippingService.LoadActiveShippingRateComputationMethods(_storeContext.CurrentStore.Id)).Any()) { if (!pickupPoints.Any()) { model.Warnings.Add(_localizationService.GetResource("Checkout.ShippingIsNotAllowed")); model.Warnings.Add(_localizationService.GetResource("Checkout.PickupPoints.NotAvailable")); } model.PickUpInStoreOnly = true; model.PickUpInStore = true; return(model); } } //existing addresses var addresses = new List <Address>(); foreach (var item in _workContext.CurrentCustomer.Addresses) { if (string.IsNullOrEmpty(item.CountryId)) { addresses.Add(item); continue; } var country = await _countryService.GetCountryById(item.CountryId); if (country == null || (country.AllowsShipping && _storeMappingService.Authorize(country))) { addresses.Add(item); continue; } } foreach (var address in addresses) { var addressModel = new AddressModel(); await _addressViewModelService.PrepareModel(model : addressModel, address : address, excludeProperties : false); model.ExistingAddresses.Add(addressModel); } //new address model.NewAddress.CountryId = selectedCountryId; var countries = await _countryService.GetAllCountriesForShipping(); await _addressViewModelService.PrepareModel(model : model.NewAddress, address : null, excludeProperties : false, loadCountries : () => countries, prePopulateWithCustomerFields : prePopulateNewAddressWithCustomerFields, customer : _workContext.CurrentCustomer, overrideAttributesXml : overrideAttributesXml); return(model); }
/// <summary> /// Prepare shipping address model /// </summary> /// <param name="selectedCountryId">Selected country identifier</param> /// <param name="prePopulateNewAddressWithCustomerFields">Pre populate new address with customer fields</param> /// <param name="overrideAttributesXml">Override attributes xml</param> /// <returns>Shipping address model</returns> public virtual CheckoutShippingAddressModel PrepareShippingAddressModel(int?selectedCountryId = null, bool prePopulateNewAddressWithCustomerFields = false, string overrideAttributesXml = "") { var model = new CheckoutShippingAddressModel(); //allow pickup in store? model.AllowPickUpInStore = _shippingSettings.AllowPickUpInStore; if (model.AllowPickUpInStore) { model.DisplayPickupPointsOnMap = _shippingSettings.DisplayPickupPointsOnMap; model.GoogleMapsApiKey = _shippingSettings.GoogleMapsApiKey; var pickupPointProviders = _shippingService.LoadActivePickupPointProviders(_workContext.CurrentCustomer, _storeContext.CurrentStore.Id); if (pickupPointProviders.Any()) { var pickupPointsResponse = _shippingService.GetPickupPoints(_workContext.CurrentCustomer.BillingAddress, _workContext.CurrentCustomer, storeId: _storeContext.CurrentStore.Id); if (pickupPointsResponse.Success) { model.PickupPoints = pickupPointsResponse.PickupPoints.Select(x => { var country = _countryService.GetCountryByTwoLetterIsoCode(x.CountryCode); var state = _stateProvinceService.GetStateProvinceByAbbreviation(x.StateAbbreviation); var pickupPointModel = new CheckoutPickupPointModel { Id = x.Id, Name = x.Name, Description = x.Description, ProviderSystemName = x.ProviderSystemName, Address = x.Address, City = x.City, StateName = state != null ? state.Name : string.Empty, CountryName = country != null ? country.Name : string.Empty, ZipPostalCode = x.ZipPostalCode, Latitude = x.Latitude, Longitude = x.Longitude, OpeningHours = x.OpeningHours }; if (x.PickupFee > 0) { var amount = _taxService.GetShippingPrice(x.PickupFee, _workContext.CurrentCustomer); amount = _currencyService.ConvertFromPrimaryStoreCurrency(amount, _workContext.WorkingCurrency); pickupPointModel.PickupFee = _priceFormatter.FormatShippingPrice(amount, true); } return(pickupPointModel); }).ToList(); } else { foreach (var error in pickupPointsResponse.Errors) { model.Warnings.Add(error); } } } //only available pickup points if (!_shippingService.LoadActiveShippingRateComputationMethods(_workContext.CurrentCustomer, _storeContext.CurrentStore.Id).Any()) { if (!pickupPointProviders.Any()) { model.Warnings.Add(_localizationService.GetResource("Checkout.ShippingIsNotAllowed")); model.Warnings.Add(_localizationService.GetResource("Checkout.PickupPoints.NotAvailable")); } model.PickUpInStoreOnly = true; model.PickUpInStore = true; return(model); } } //existing addresses var addresses = _workContext.CurrentCustomer.Addresses .Where(a => a.Country == null || (//published a.Country.Published && //allow shipping a.Country.AllowsShipping && //enabled for the current store _storeMappingService.Authorize(a.Country))) .ToList(); foreach (var address in addresses) { var addressModel = new AddressModel(); _addressModelFactory.PrepareAddressModel(addressModel, address: address, excludeProperties: false, addressSettings: _addressSettings); model.ExistingAddresses.Add(addressModel); } //new address model.NewAddress.CountryId = selectedCountryId; _addressModelFactory.PrepareAddressModel(model.NewAddress, address: null, excludeProperties: false, addressSettings: _addressSettings, loadCountries: () => _countryService.GetAllCountriesForShipping(_workContext.WorkingLanguage.Id), prePopulateWithCustomerFields: prePopulateNewAddressWithCustomerFields, customer: _workContext.CurrentCustomer, overrideAttributesXml: overrideAttributesXml); return(model); }
/// <summary> /// Prepares the checkout pickup points model /// </summary> /// <param name="cart">Cart</param> /// <returns> /// A task that represents the asynchronous operation /// The task result contains the checkout pickup points model /// </returns> protected virtual async Task <CheckoutPickupPointsModel> PrepareCheckoutPickupPointsModelAsync(IList <ShoppingCartItem> cart) { var model = new CheckoutPickupPointsModel { AllowPickupInStore = _shippingSettings.AllowPickupInStore }; if (!model.AllowPickupInStore) { return(model); } model.DisplayPickupPointsOnMap = _shippingSettings.DisplayPickupPointsOnMap; model.GoogleMapsApiKey = _shippingSettings.GoogleMapsApiKey; var pickupPointProviders = await _pickupPluginManager.LoadActivePluginsAsync(await _workContext.GetCurrentCustomerAsync(), (await _storeContext.GetCurrentStoreAsync()).Id); if (pickupPointProviders.Any()) { var languageId = (await _workContext.GetWorkingLanguageAsync()).Id; var pickupPointsResponse = await _shippingService.GetPickupPointsAsync((await _workContext.GetCurrentCustomerAsync()).BillingAddressId ?? 0, await _workContext.GetCurrentCustomerAsync(), storeId : (await _storeContext.GetCurrentStoreAsync()).Id); if (pickupPointsResponse.Success) { model.PickupPoints = await pickupPointsResponse.PickupPoints.SelectAwait(async point => { var country = await _countryService.GetCountryByTwoLetterIsoCodeAsync(point.CountryCode); var state = await _stateProvinceService.GetStateProvinceByAbbreviationAsync(point.StateAbbreviation, country?.Id); var pickupPointModel = new CheckoutPickupPointModel { Id = point.Id, Name = point.Name, Description = point.Description, ProviderSystemName = point.ProviderSystemName, Address = point.Address, City = point.City, County = point.County, StateName = state != null ? await _localizationService.GetLocalizedAsync(state, x => x.Name, languageId) : string.Empty, CountryName = country != null ? await _localizationService.GetLocalizedAsync(country, x => x.Name, languageId) : string.Empty, ZipPostalCode = point.ZipPostalCode, Latitude = point.Latitude, Longitude = point.Longitude, OpeningHours = point.OpeningHours }; var cart = await _shoppingCartService.GetShoppingCartAsync(await _workContext.GetCurrentCustomerAsync(), ShoppingCartType.ShoppingCart, (await _storeContext.GetCurrentStoreAsync()).Id); var amount = await _orderTotalCalculationService.IsFreeShippingAsync(cart) ? 0 : point.PickupFee; if (amount > 0) { (amount, _) = await _taxService.GetShippingPriceAsync(amount, await _workContext.GetCurrentCustomerAsync()); amount = await _currencyService.ConvertFromPrimaryStoreCurrencyAsync(amount, await _workContext.GetWorkingCurrencyAsync()); pickupPointModel.PickupFee = await _priceFormatter.FormatShippingPriceAsync(amount, true); } //adjust rate var(shippingTotal, _) = await _orderTotalCalculationService.AdjustShippingRateAsync(point.PickupFee, cart, true); var(rateBase, _) = await _taxService.GetShippingPriceAsync(shippingTotal, await _workContext.GetCurrentCustomerAsync()); var rate = await _currencyService.ConvertFromPrimaryStoreCurrencyAsync(rateBase, await _workContext.GetWorkingCurrencyAsync()); pickupPointModel.PickupFee = await _priceFormatter.FormatShippingPriceAsync(rate, true); return(pickupPointModel); }).ToListAsync(); } else { foreach (var error in pickupPointsResponse.Errors) { model.Warnings.Add(error); } } } //only available pickup points var shippingProviders = await _shippingPluginManager.LoadActivePluginsAsync(await _workContext.GetCurrentCustomerAsync(), (await _storeContext.GetCurrentStoreAsync()).Id); if (!shippingProviders.Any()) { if (!pickupPointProviders.Any()) { model.Warnings.Add(await _localizationService.GetResourceAsync("Checkout.ShippingIsNotAllowed")); model.Warnings.Add(await _localizationService.GetResourceAsync("Checkout.PickupPoints.NotAvailable")); } model.PickupInStoreOnly = true; model.PickupInStore = true; return(model); } return(model); }
public virtual CheckoutShippingAddressModel PrepareShippingAddress(string selectedCountryId = null, bool prePopulateNewAddressWithCustomerFields = false, string overrideAttributesXml = "") { var model = new CheckoutShippingAddressModel(); //allow pickup in store? model.AllowPickUpInStore = _shippingSettings.AllowPickUpInStore; if (model.AllowPickUpInStore) { var pickupPoints = _shippingService.LoadActivePickupPoints(_storeContext.CurrentStore.Id); if (pickupPoints.Any()) { model.PickupPoints = pickupPoints.Select(x => { var pickupPointModel = new CheckoutPickupPointModel() { Id = x.Id, Name = x.Name, Description = x.Description, Address = x.Address, }; if (x.PickupFee > 0) { var amount = _taxService.GetShippingPrice(x.PickupFee, _workContext.CurrentCustomer); amount = _currencyService.ConvertFromPrimaryStoreCurrency(amount, _workContext.WorkingCurrency); pickupPointModel.PickupFee = _priceFormatter.FormatShippingPrice(amount, true); } return(pickupPointModel); }).ToList(); } if (!_shippingService.LoadActiveShippingRateComputationMethods(_storeContext.CurrentStore.Id).Any()) { if (!pickupPoints.Any()) { model.Warnings.Add(_localizationService.GetResource("Checkout.ShippingIsNotAllowed")); model.Warnings.Add(_localizationService.GetResource("Checkout.PickupPoints.NotAvailable")); } model.PickUpInStoreOnly = true; model.PickUpInStore = true; return(model); } } //existing addresses var addresses = _workContext.CurrentCustomer.Addresses //allow shipping .Where(a => a.CountryId == "" || (_countryService.GetCountryById(a.CountryId) != null ? _countryService.GetCountryById(a.CountryId).AllowsShipping : false) //a.Country.AllowsShipping ) //enabled for the current store .Where(a => a.CountryId == "" || _storeMappingService.Authorize(_countryService.GetCountryById(a.CountryId))) .ToList(); foreach (var address in addresses) { var addressModel = new AddressModel(); _addressViewModelService.PrepareModel(model: addressModel, address: address, excludeProperties: false); model.ExistingAddresses.Add(addressModel); } //new address model.NewAddress.CountryId = selectedCountryId; _addressViewModelService.PrepareModel(model: model.NewAddress, address: null, excludeProperties: false, loadCountries: () => _countryService.GetAllCountriesForShipping(), prePopulateWithCustomerFields: prePopulateNewAddressWithCustomerFields, customer: _workContext.CurrentCustomer, overrideAttributesXml: overrideAttributesXml); return(model); }
/// <summary> /// Prepares the checkout pickup points model /// </summary> /// <returns>The checkout pickup points model</returns> protected virtual CheckoutPickupPointsModel PrepareCheckoutPickupPointsModel() { var model = new CheckoutPickupPointsModel() { AllowPickupInStore = _shippingSettings.AllowPickupInStore }; if (model.AllowPickupInStore) { model.DisplayPickupPointsOnMap = _shippingSettings.DisplayPickupPointsOnMap; model.GoogleMapsApiKey = _shippingSettings.GoogleMapsApiKey; var pickupPointProviders = _pickupPluginManager.LoadActivePlugins(_workContext.CurrentCustomer, _storeContext.CurrentStore.Id); if (pickupPointProviders.Any()) { var languageId = _workContext.WorkingLanguage.Id; var pickupPointsResponse = _shippingService.GetPickupPoints(_workContext.CurrentCustomer.BillingAddressId ?? 0, _workContext.CurrentCustomer, storeId: _storeContext.CurrentStore.Id); if (pickupPointsResponse.Success) { model.PickupPoints = pickupPointsResponse.PickupPoints.Select(point => { var country = _countryService.GetCountryByTwoLetterIsoCode(point.CountryCode); var state = _stateProvinceService.GetStateProvinceByAbbreviation(point.StateAbbreviation, country?.Id); var pickupPointModel = new CheckoutPickupPointModel { Id = point.Id, Name = point.Name, Description = point.Description, ProviderSystemName = point.ProviderSystemName, Address = point.Address, City = point.City, County = point.County, StateName = state != null ? _localizationService.GetLocalized(state, x => x.Name, languageId) : string.Empty, CountryName = country != null ? _localizationService.GetLocalized(country, x => x.Name, languageId) : string.Empty, ZipPostalCode = point.ZipPostalCode, Latitude = point.Latitude, Longitude = point.Longitude, OpeningHours = point.OpeningHours }; if (point.PickupFee > 0) { var amount = _taxService.GetShippingPrice(point.PickupFee, _workContext.CurrentCustomer); amount = _currencyService.ConvertFromPrimaryStoreCurrency(amount, _workContext.WorkingCurrency); pickupPointModel.PickupFee = _priceFormatter.FormatShippingPrice(amount, true); } return(pickupPointModel); }).ToList(); } else { foreach (var error in pickupPointsResponse.Errors) { model.Warnings.Add(error); } } } //only available pickup points var shippingProviders = _shippingPluginManager.LoadActivePlugins(_workContext.CurrentCustomer, _storeContext.CurrentStore.Id); if (!shippingProviders.Any()) { if (!pickupPointProviders.Any()) { model.Warnings.Add(_localizationService.GetResource("Checkout.ShippingIsNotAllowed")); model.Warnings.Add(_localizationService.GetResource("Checkout.PickupPoints.NotAvailable")); } model.PickupInStoreOnly = true; model.PickupInStore = true; return(model); } } return(model); }