/// <summary> /// Migrate shopping cart /// </summary> /// <param name="fromCustomer">From customer</param> /// <param name="toCustomer">To customer</param> /// <param name="includeCouponCodes">A value indicating whether to coupon codes (discount and gift card) should be also re-applied</param> public virtual void MigrateShoppingCart(Customer fromCustomer, Customer toCustomer, bool includeCouponCodes) { if (fromCustomer == null) throw new ArgumentNullException("fromCustomer"); if (toCustomer == null) throw new ArgumentNullException("toCustomer"); if (fromCustomer.Id == toCustomer.Id) return; //the same customer //shopping cart items var fromCart = fromCustomer.ShoppingCartItems.ToList(); for (int i = 0; i < fromCart.Count; i++) { var sci = fromCart[i]; AddToCart(toCustomer, sci.ProductVariant, sci.ShoppingCartType, sci.StoreId, sci.AttributesXml, sci.CustomerEnteredPrice, sci.Quantity, false); } for (int i = 0; i < fromCart.Count; i++) { var sci = fromCart[i]; DeleteShoppingCartItem(sci); } //migrate gift card and discount coupon codes if (includeCouponCodes) { //discount var discountCouponCode = fromCustomer.GetAttribute<string>(SystemCustomerAttributeNames.DiscountCouponCode); if (!String.IsNullOrEmpty(discountCouponCode)) _genericAttributeService.SaveAttribute(toCustomer, SystemCustomerAttributeNames.DiscountCouponCode, discountCouponCode); //gift card foreach (var gcCode in fromCustomer.ParseAppliedGiftCardCouponCodes()) toCustomer.ApplyGiftCardCouponCode(gcCode); //save customer _customerService.UpdateCustomer(toCustomer); } }
protected void PrepareCustomerInfoModel(CustomerInfoModel model, Customer customer, bool excludeProperties) { if (model == null) throw new ArgumentNullException("model"); if (customer == null) throw new ArgumentNullException("customer"); model.AllowCustomersToSetTimeZone = _dateTimeSettings.AllowCustomersToSetTimeZone; foreach (var tzi in _dateTimeHelper.GetSystemTimeZones()) model.AvailableTimeZones.Add(new SelectListItem() { Text = tzi.DisplayName, Value = tzi.Id, Selected = (excludeProperties ? tzi.Id == model.TimeZoneId : tzi.Id == _dateTimeHelper.CurrentTimeZone.Id) }); if (!excludeProperties) { model.VatNumber = customer.GetAttribute<string>(SystemCustomerAttributeNames.VatNumber); model.FirstName = customer.GetAttribute<string>(SystemCustomerAttributeNames.FirstName); model.LastName = customer.GetAttribute<string>(SystemCustomerAttributeNames.LastName); model.Gender = customer.GetAttribute<string>(SystemCustomerAttributeNames.Gender); var dateOfBirth = customer.GetAttribute<DateTime?>(SystemCustomerAttributeNames.DateOfBirth); if (dateOfBirth.HasValue) { model.DateOfBirthDay = dateOfBirth.Value.Day; model.DateOfBirthMonth = dateOfBirth.Value.Month; model.DateOfBirthYear = dateOfBirth.Value.Year; } model.Company = customer.GetAttribute<string>(SystemCustomerAttributeNames.Company); model.StreetAddress = customer.GetAttribute<string>(SystemCustomerAttributeNames.StreetAddress); model.StreetAddress2 = customer.GetAttribute<string>(SystemCustomerAttributeNames.StreetAddress2); model.ZipPostalCode = customer.GetAttribute<string>(SystemCustomerAttributeNames.ZipPostalCode); model.City = customer.GetAttribute<string>(SystemCustomerAttributeNames.City); model.CountryId = customer.GetAttribute<int>(SystemCustomerAttributeNames.CountryId); model.StateProvinceId = customer.GetAttribute<int>(SystemCustomerAttributeNames.StateProvinceId); model.Phone = customer.GetAttribute<string>(SystemCustomerAttributeNames.Phone); model.Fax = customer.GetAttribute<string>(SystemCustomerAttributeNames.Fax); //newsletter var newsletter = _newsLetterSubscriptionService.GetNewsLetterSubscriptionByEmail(customer.Email); model.Newsletter = newsletter != null && newsletter.Active; model.Signature = customer.GetAttribute<string>(SystemCustomerAttributeNames.Signature); model.Email = customer.Email; model.Username = customer.Username; } else { if (_customerSettings.UsernamesEnabled && !_customerSettings.AllowUsersToChangeUsernames) model.Username = customer.Username; } //countries and states if (_customerSettings.CountryEnabled) { model.AvailableCountries.Add(new SelectListItem() { Text = _localizationService.GetResource("Address.SelectCountry"), Value = "0" }); foreach (var c in _countryService.GetAllCountries()) { model.AvailableCountries.Add(new SelectListItem() { Text = c.GetLocalized(x => x.Name), Value = c.Id.ToString(), Selected = c.Id == model.CountryId }); } if (_customerSettings.StateProvinceEnabled) { //states var states = _stateProvinceService.GetStateProvincesByCountryId(model.CountryId).ToList(); if (states.Count > 0) { foreach (var s in states) model.AvailableStates.Add(new SelectListItem() { Text = s.GetLocalized(x => x.Name), Value = s.Id.ToString(), Selected = (s.Id == model.StateProvinceId) }); } else model.AvailableStates.Add(new SelectListItem() { Text = _localizationService.GetResource("Address.OtherNonUS"), Value = "0" }); } } model.DisplayVatNumber = _taxSettings.EuVatEnabled; model.VatNumberStatusNote = ((VatNumberStatus)customer.GetAttribute<int>(SystemCustomerAttributeNames.VatNumberStatusId)) .GetLocalizedEnum(_localizationService, _workContext); model.GenderEnabled = _customerSettings.GenderEnabled; model.DateOfBirthEnabled = _customerSettings.DateOfBirthEnabled; model.CompanyEnabled = _customerSettings.CompanyEnabled; model.CompanyRequired = _customerSettings.CompanyRequired; model.StreetAddressEnabled = _customerSettings.StreetAddressEnabled; model.StreetAddressRequired = _customerSettings.StreetAddressRequired; model.StreetAddress2Enabled = _customerSettings.StreetAddress2Enabled; model.StreetAddress2Required = _customerSettings.StreetAddress2Required; model.ZipPostalCodeEnabled = _customerSettings.ZipPostalCodeEnabled; model.ZipPostalCodeRequired = _customerSettings.ZipPostalCodeRequired; model.CityEnabled = _customerSettings.CityEnabled; model.CityRequired = _customerSettings.CityRequired; model.CountryEnabled = _customerSettings.CountryEnabled; model.StateProvinceEnabled = _customerSettings.StateProvinceEnabled; model.PhoneEnabled = _customerSettings.PhoneEnabled; model.PhoneRequired = _customerSettings.PhoneRequired; model.FaxEnabled = _customerSettings.FaxEnabled; model.FaxRequired = _customerSettings.FaxRequired; model.NewsletterEnabled = _customerSettings.NewsletterEnabled; model.UsernamesEnabled = _customerSettings.UsernamesEnabled; model.AllowUsersToChangeUsernames = _customerSettings.AllowUsersToChangeUsernames; model.CheckUsernameAvailabilityEnabled = _customerSettings.CheckUsernameAvailabilityEnabled; model.SignatureEnabled = _forumSettings.ForumsEnabled && _forumSettings.SignaturesEnabled; //external authentication foreach (var ear in _openAuthenticationService.GetExternalIdentifiersFor(customer)) { var authMethod = _openAuthenticationService.LoadExternalAuthenticationMethodBySystemName(ear.ProviderSystemName); if (authMethod == null || !authMethod.IsMethodActive(_externalAuthenticationSettings)) continue; model.AssociatedExternalAuthRecords.Add(new CustomerInfoModel.AssociatedExternalAuthModel() { Id = ear.Id, Email = ear.Email, ExternalIdentifier = ear.ExternalIdentifier, AuthMethodName = authMethod.GetLocalizedFriendlyName(_localizationService, _workContext.WorkingLanguage.Id) }); } model.NavigationModel = GetCustomerNavigationModel(customer); model.NavigationModel.SelectedTab = CustomerNavigationEnum.Info; }
protected CustomerModel PrepareCustomerModelForList(Customer customer) { return new CustomerModel() { Id = customer.Id, Email = customer.IsRegistered() ? customer.Email : _localizationService.GetResource("Admin.Customers.Guest"), Username = customer.Username, FullName = customer.GetFullName(), Company = customer.GetAttribute<string>(SystemCustomerAttributeNames.Company), Phone = customer.GetAttribute<string>(SystemCustomerAttributeNames.Phone), ZipPostalCode = customer.GetAttribute<string>(SystemCustomerAttributeNames.ZipPostalCode), CustomerRoleNames = GetCustomerRolesNames(customer.CustomerRoles.ToList()), Active = customer.Active, CreatedOn = _dateTimeHelper.ConvertToUserTime(customer.CreatedOnUtc, DateTimeKind.Utc), LastActivityDate = _dateTimeHelper.ConvertToUserTime(customer.LastActivityDateUtc, DateTimeKind.Utc), }; }
public virtual void AddCustomerTokens(IList<Token> tokens, Customer customer) { tokens.Add(new Token("Customer.Email", customer.Email)); tokens.Add(new Token("Customer.Username", customer.Username)); tokens.Add(new Token("Customer.FullName", customer.GetFullName())); tokens.Add(new Token("Customer.VatNumber", customer.GetAttribute<string>(SystemCustomerAttributeNames.VatNumber))); tokens.Add(new Token("Customer.VatNumberStatus", ((VatNumberStatus)customer.GetAttribute<int>(SystemCustomerAttributeNames.VatNumberStatusId)).ToString())); //note: we do not use SEO friendly URLS because we can get errors caused by having .(dot) in the URL (from the email address) //TODO add a method for getting URL (use routing because it handles all SEO friendly URLs) string passwordRecoveryUrl = string.Format("{0}passwordrecovery/confirm?token={1}&email={2}", _webHelper.GetStoreLocation(false), customer.GetAttribute<string>(SystemCustomerAttributeNames.PasswordRecoveryToken), HttpUtility.UrlEncode(customer.Email)); string accountActivationUrl = string.Format("{0}customer/activation?token={1}&email={2}", _webHelper.GetStoreLocation(false), customer.GetAttribute<string>(SystemCustomerAttributeNames.AccountActivationToken), HttpUtility.UrlEncode(customer.Email)); var wishlistUrl = string.Format("{0}wishlist/{1}", _webHelper.GetStoreLocation(false), customer.CustomerGuid); tokens.Add(new Token("Customer.PasswordRecoveryURL", passwordRecoveryUrl, true)); tokens.Add(new Token("Customer.AccountActivationURL", accountActivationUrl, true)); tokens.Add(new Token("Wishlist.URLForCustomer", wishlistUrl, true)); //event notification _eventPublisher.EntityTokensAdded(customer, tokens); }
/// <summary> /// Check discount requirements /// </summary> /// <param name="discount">Discount</param> /// <param name="customer">Customer</param> /// <returns>true - requirement is met; otherwise, false</returns> public virtual bool IsDiscountValid(Discount discount, Customer customer) { if (discount == null) throw new ArgumentNullException("discount"); var couponCodeToValidate = ""; if (customer != null) couponCodeToValidate = customer.GetAttribute<string>(SystemCustomerAttributeNames.DiscountCouponCode, _genericAttributeService); return IsDiscountValid(discount, customer, couponCodeToValidate); }
/// <summary> /// Gets a customer time zone /// </summary> /// <param name="customer">Customer</param> /// <returns>Customer time zone; if customer is null, then default store time zone</returns> public virtual TimeZoneInfo GetCustomerTimeZone(Customer customer) { //registered user TimeZoneInfo timeZoneInfo = null; if (_dateTimeSettings.AllowCustomersToSetTimeZone) { string timeZoneId = string.Empty; if (customer != null) timeZoneId = customer.GetAttribute<string>(SystemCustomerAttributeNames.TimeZoneId, _genericAttributeService); try { if (!String.IsNullOrEmpty(timeZoneId)) timeZoneInfo = FindTimeZoneById(timeZoneId); } catch (Exception exc) { Debug.Write(exc.ToString()); } } //default timezone if (timeZoneInfo == null) timeZoneInfo = this.DefaultStoreTimeZone; return timeZoneInfo; }
/// <summary> /// Gets a value indicating whether EU VAT exempt (the European Union Value Added Tax) /// </summary> /// <param name="address">Address</param> /// <param name="customer">Customer</param> /// <returns>Result</returns> public virtual bool IsVatExempt(Address address, Customer customer) { if (!_taxSettings.EuVatEnabled) { return false; } if (address == null || address.Country == null || customer == null) { return false; } if (!address.Country.SubjectToVat) { // VAT not chargeable if shipping outside VAT zone: return true; } else { // VAT not chargeable if address, customer and config meet our VAT exemption requirements: // returns true if this customer is VAT exempt because they are shipping within the EU but outside our shop country, they have supplied a validated VAT number, and the shop is configured to allow VAT exemption var customerVatStatus = (VatNumberStatus)customer.GetAttribute<int>(SystemCustomerAttributeNames.VatNumberStatusId); return address.CountryId != _taxSettings.EuVatShopCountryId && customerVatStatus == VatNumberStatus.Valid && _taxSettings.EuVatAllowVatExemption; } }