Exemple #1
0
        protected CheckoutShippingAddressModel PrepareShippingAddressModel(int?selectedCountryId = null)
        {
            var model = new CheckoutShippingAddressModel();
            //existing addresses
            var addresses = _workContext.CurrentCustomer.Addresses.Where(a => a.Country == null || a.Country.AllowsShipping).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.GetAllCountriesForShipping());
            return(model);
        }
        /// <summary>
        /// Prepare shipping address model
        /// </summary>
        /// <param name="cart">Cart</param>
        /// <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>
        /// A task that represents the asynchronous operation
        /// The task result contains the shipping address model
        /// </returns>
        public virtual async Task <CheckoutShippingAddressModel> PrepareShippingAddressModelAsync(IList <ShoppingCartItem> cart,
                                                                                                  int?selectedCountryId = null, bool prePopulateNewAddressWithCustomerFields = false, string overrideAttributesXml = "")
        {
            var model = new CheckoutShippingAddressModel
            {
                DisplayPickupInStore = !_orderSettings.DisplayPickupInStoreOnShippingMethodPage
            };

            if (!_orderSettings.DisplayPickupInStoreOnShippingMethodPage)
            {
                model.PickupPointsModel = await PrepareCheckoutPickupPointsModelAsync(cart);
            }

            //existing addresses
            var customer = await _workContext.GetCurrentCustomerAsync();

            var addresses = await(await _customerService.GetAddressesByCustomerIdAsync(customer.Id))
                            .WhereAwait(async a => !a.CountryId.HasValue || await _countryService.GetCountryByAddressAsync(a) is Country country &&
                                        (//published
                                            country.Published &&
                                            //allow shipping
                                            country.AllowsShipping &&
                                            //enabled for the current store
                                            await _storeMappingService.AuthorizeAsync(country)))
                            .ToListAsync();

            foreach (var address in addresses)
            {
                var addressModel = new AddressModel();
                await _addressModelFactory.PrepareAddressModelAsync(addressModel,
                                                                    address : address,
                                                                    excludeProperties : false,
                                                                    addressSettings : _addressSettings);

                if (await _addressService.IsAddressValidAsync(address))
                {
                    model.ExistingAddresses.Add(addressModel);
                }
                else
                {
                    model.InvalidExistingAddresses.Add(addressModel);
                }
            }

            //new address
            model.ShippingNewAddress.CountryId = selectedCountryId;
            await _addressModelFactory.PrepareAddressModelAsync(model.ShippingNewAddress,
                                                                address : null,
                                                                excludeProperties : false,
                                                                addressSettings : _addressSettings,
                                                                loadCountries : async() => await _countryService.GetAllCountriesForShippingAsync((await _workContext.GetWorkingLanguageAsync()).Id),
                                                                prePopulateWithCustomerFields : prePopulateNewAddressWithCustomerFields,
                                                                customer : customer,
                                                                overrideAttributesXml : overrideAttributesXml);

            return(model);
        }
Exemple #3
0
        /// <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()
            {
                DisplayPickupInStore = !_orderSettings.DisplayPickupInStoreOnShippingMethodPage
            };

            if (!_orderSettings.DisplayPickupInStoreOnShippingMethodPage)
            {
                model.PickupPointsModel = PrepareCheckoutPickupPointsModel();
            }

            //existing addresses
            var addresses = _customerService.GetAddressesByCustomerId(_workContext.CurrentCustomer.Id)
                            .Where(a => _countryService.GetCountryByAddress(a) is Country country &&
                                   (//published
                                       country.Published &&
                                       //allow shipping
                                       country.AllowsShipping &&
                                       //enabled for the current store
                                       _storeMappingService.Authorize(country)))
                            .ToList();

            foreach (var address in addresses)
            {
                var addressModel = new AddressModel();
                _addressModelFactory.PrepareAddressModel(addressModel,
                                                         address: address,
                                                         excludeProperties: false,
                                                         addressSettings: _addressSettings);

                if (_addressService.IsAddressValid(address))
                {
                    model.ExistingAddresses.Add(addressModel);
                }
                else
                {
                    model.InvalidExistingAddresses.Add(addressModel);
                }
            }

            //new address
            model.ShippingNewAddress.CountryId = selectedCountryId;
            _addressModelFactory.PrepareAddressModel(model.ShippingNewAddress,
                                                     address: null,
                                                     excludeProperties: false,
                                                     addressSettings: _addressSettings,
                                                     loadCountries: () => _countryService.GetAllCountriesForShipping(_workContext.WorkingLanguage.Id),
                                                     prePopulateWithCustomerFields: prePopulateNewAddressWithCustomerFields,
                                                     customer: _workContext.CurrentCustomer,
                                                     overrideAttributesXml: overrideAttributesXml);

            return(model);
        }
Exemple #4
0
        public ActionResult NewShippingAddress(CheckoutShippingAddressModel model)
        {
            //validation
            var cart = _workContext.CurrentCustomer.GetCartItems(ShoppingCartType.ShoppingCart, _storeContext.CurrentStore.Id);

            if (cart.Count == 0)
            {
                return(RedirectToRoute("ShoppingCart"));
            }

            if ((_workContext.CurrentCustomer.IsGuest() && !_orderSettings.AnonymousCheckoutAllowed))
            {
                return(new HttpUnauthorizedResult());
            }

            if (!cart.RequiresShipping())
            {
                _workContext.CurrentCustomer.ShippingAddress = null;
                _customerService.UpdateCustomer(_workContext.CurrentCustomer);
                return(RedirectToAction("ShippingMethod"));
            }

            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.ShippingAddress = address;
                _customerService.UpdateCustomer(_workContext.CurrentCustomer);

                return(RedirectToAction("ShippingMethod"));
            }


            //If we got this far, something failed, redisplay form
            model = PrepareShippingAddressModel(model.NewAddress.CountryId);
            return(View(model));
        }
        public bool SetNewAddress(CheckoutShippingAddressModel checkoutShippingAddressModel)
        {
            var address = checkoutShippingAddressModel.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.ShippingAddress = address;
            _customerService.UpdateCustomer(_workContext.CurrentCustomer);
            return(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);
        }
Exemple #8
0
        public ActionResult NewOrderShippingCartAddress(int recepientId, CheckoutShippingAddressModel model, FormCollection form)
        {
            //validation
            var shippingCart =
                _workContext.CurrentCustomer.ShippingCarts.FirstOrDefault(sci => sci.Id == recepientId);

            if (shippingCart == null || shippingCart.ShippingCartItems.Count == 0)
            {
                return(RedirectToRoute("ShoppingCart"));
            }

            if (_orderSettings.OnePageCheckoutEnabled)
            {
                return(RedirectToRoute("CheckoutOnePage"));
            }

            if ((_workContext.CurrentCustomer.IsGuest() && !_orderSettings.AnonymousCheckoutAllowed))
            {
                return(new HttpUnauthorizedResult());
            }

            if (!shippingCart.RequiresShipping())
            {
                shippingCart.ShippingAddress = null;
                _customerService.UpdateCustomer(_workContext.CurrentCustomer);
                return(GetNextShippingCartAction(shippingCart.Id));
            }


            //Pick up in store?
            if (_shippingSettings.AllowPickUpInStore)
            {
                if (model.PickUpInStore)
                {
                    //customer decided to pick up in store

                    //no shipping address selected
                    shippingCart.ShippingAddress = null;
                    _customerService.UpdateCustomer(_workContext.CurrentCustomer);

                    //set value indicating that "pick up in store" option has been chosen
                    _genericAttributeService.SaveAttribute(shippingCart, SystemCustomerAttributeNames.SelectedPickUpInStore, true, _storeContext.CurrentStore.Id);

                    //save "pick up in store" shipping method
                    var pickUpInStoreShippingOption = new ShippingOption
                    {
                        Name        = _localizationService.GetResource("Checkout.PickUpInStore.MethodName"),
                        Rate        = _shippingSettings.PickUpInStoreFee,
                        Description = null,
                        ShippingRateComputationMethodSystemName = null
                    };
                    _genericAttributeService.SaveAttribute(shippingCart,
                                                           SystemCustomerAttributeNames.SelectedShippingOption,
                                                           pickUpInStoreShippingOption,
                                                           _storeContext.CurrentStore.Id);

                    //load next step
                    return(GetNextShippingCartAction(shippingCart.Id));
                }

                //set value indicating that "pick up in store" option has not been chosen
                _genericAttributeService.SaveAttribute(shippingCart, SystemCustomerAttributeNames.SelectedPickUpInStore, false, _storeContext.CurrentStore.Id);
            }

            //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,
                    model.NewAddress.RecipientTitle, model.NewAddress.RecipientName);
                if (address == null)
                {
                    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);
                }
                shippingCart.ShippingAddress = address;

                shippingCart.PickUpInStore          = model.PickUpInStore;
                shippingCart.ExpectedDeliveryDate   = model.ExpectedDeliveryDate;
                shippingCart.ExpectedDeliveryPeriod = model.ExpectedDeliveryPeriod;

                //update Gretting message
                shippingCart.GreetingType = model.GreetingType;
                shippingCart.From         = model.From;
                shippingCart.To           = model.To;
                shippingCart.Message      = model.Message;

                _customerService.UpdateCustomer(_workContext.CurrentCustomer);

                return(GetNextShippingCartAction(shippingCart.Id));
            }


            //If we got this far, something failed, redisplay form
            model = PrepareShippingAddressModel(selectedCountryId: model.NewAddress.CountryId);
            return(View(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);
        }
Exemple #10
0
 public CheckoutAddressModel()
 {
     CheckoutShippingAddressModel = new CheckoutShippingAddressModel();
     CheckoutBillingAddressModel  = new CheckoutBillingAddressModel();
 }