AI agents: the content index is at /llms.txt. Every page is also available as Markdown — send the request header Accept: text/markdown, or append .md to the page URL.
Xperience Docs

Cookie consent

We use necessary cookies (opens in a new tab) to run our website and improve your experience while browsing. Additional cookies are only used with your consent. You may revoke your consent on the Cookies Policy (opens in a new tab) page or in your browser at any time.

Configure (opens in a new tab)
  • Documentation
    InstallationUsing the Xperience interfaceDeveloping websitesConfiguring XperienceManaging usersSecuring websitesDeploying websitesManaging website contentMultilingual websitesMacro expressionsExternal utilitiesIntegrating 3rd party systemsCustom developmentE-commerce featuresOn-line marketing featuresRelease notes - Xperience 13Xperience terminology glossary
  • Tutorial
    Using the Xperience interfaceDeveloper tutorialBusiness tutorialResource hub for marketers
  • API Examples
    ConfigurationContent managementDevelopmentE-commerceOn-line marketingSocial media
  1. Home
  2. E-commerce
  3. Customers

Customers


Was this page helpful?

Send us feedback Go back to top
  • Community Questions & Answers (opens in a new tab)
  • Contact support (opens in a new tab)

List of examples:

  • Customers
    • Creating an anonymous customer
    • Creating a registered customer
    • Updating a customer
    • Updating multiple customers
    • Deleting customers
  • Customer addresses
    • Creating an address
    • Updating an address
    • Updating multiple addresses
    • Deleting an address
  • Customer newsletter subscriptions
    • Subscribing customers to newsletters
    • Unsubscribing customers from newsletters
  • Customer wishlists
    • Adding products to a wishlist
    • Removing products from a wishlist

Customers

Creating an anonymous customer

C#
// Creates a new customer object
CustomerInfo newCustomer = new CustomerInfo
{
    // Sets the customer properties
    CustomerFirstName = "Joe",
    CustomerLastName = "Smith",
    CustomerEmail = "smith@localhost.local",
    CustomerSiteID = SiteContext.CurrentSiteID
};

// Saves the anonymous customer to the database
CustomerInfo.Provider.Set(newCustomer);
> Back to list of examples

Creating a registered customer

C#
// Creates a new user
UserInfo newUser = new UserInfo
{
    // Sets the user properties
    UserName = "Smith",
    UserEnabled = true
};

// Sets the user's privilege level
newUser.SiteIndependentPrivilegeLevel = UserPrivilegeLevelEnum.Editor;

// Saves the user to the database
UserInfo.Provider.Set(newUser);

// Assigns the user to the current site
UserInfoProvider.AddUserToSite(newUser.UserName, SiteContext.CurrentSiteName);

// Creates a new customer object
CustomerInfo newCustomer = new CustomerInfo
{
    // Sets the customer properties (e.g. assigns the customer to the user)
    CustomerFirstName = "Joe",
    CustomerLastName = "Smith",
    CustomerEmail = "smith@localhost.local",
    CustomerSiteID = SiteContext.CurrentSiteID,
    CustomerUserID = newUser.UserID
};

// Saves the registered customer to the database
CustomerInfo.Provider.Set(newCustomer);
> Back to list of examples

Updating a customer

C#
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfo.Provider.Get()
                                                .WhereEquals("CustomerLastName", "Smith")
                                                .TopN(1)
                                                .FirstOrDefault();
if (customer != null)
{
    // Updates the customer's properties
    customer.CustomerLastName = customer.CustomerLastName.ToLowerCSafe();

    // Saves the changes to the database
    CustomerInfo.Provider.Set(customer);
}
> Back to list of examples

Updating multiple customers

C#
// Gets all customers whose last name starts with 'Sm'
var customers = CustomerInfo.Provider.Get().WhereStartsWith("CustomerLastName", "Sm");

// Loops through the customers
foreach (CustomerInfo modifyCustomer in customers)
{
    // Updates the customer's properties
    modifyCustomer.CustomerLastName = modifyCustomer.CustomerLastName.ToUpperCSafe();

    // Saves the changes to the database
    CustomerInfo.Provider.Set(modifyCustomer);
}
> Back to list of examples

Deleting customers

C#
// Gets all customers whose last name starts with 'Sm'
var customers = CustomerInfo.Provider.Get().WhereStartsWith("CustomerLastName", "Sm");

// Loops through the customers
foreach (CustomerInfo deleteCustomer in customers)
{
    // Deletes the related user for registered customers
    UserInfo user = deleteCustomer.CustomerUser;
    if (user != null)
    {
        UserInfo.Provider.Delete(user);
    }

    // Deletes the customer
    CustomerInfo.Provider.Delete(deleteCustomer);
}
> Back to list of examples

Customer addresses

Creating an address

C#
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfo.Provider.Get()
                                            .WhereEquals("CustomerLastName", "Smith")
                                            .TopN(1)
                                            .FirstOrDefault();

// Gets a country for the address
CountryInfo country = CountryInfo.Provider.Get("USA");

// Gets a state for the address
StateInfo state = StateInfo.Provider.Get("Alabama");

if ((customer != null) && (country != null))
{
    // Creates a new address object and sets its properties
    AddressInfo newAddress = new AddressInfo
    {
        AddressName = "New address",
        AddressLine1 = "Address line 1",
        AddressLine2 = "Address line 2",
        AddressCity = "Address city",
        AddressZip = "Address ZIP code",
        AddressPersonalName = customer.CustomerInfoName,
        AddressCustomerID = customer.CustomerID,
        AddressCountryID = country.CountryID,
        AddressStateID = state.StateID
    };

    // Saves the address to the dataabase
    AddressInfo.Provider.Set(newAddress);
}
> Back to list of examples

Updating an address

C#
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfo.Provider.Get()
                                            .WhereEquals("CustomerLastName", "Smith")
                                            .TopN(1)
                                            .FirstOrDefault();

if (customer != null)
{
    // Gets the first enabled address stored for the customer
    AddressInfo address = AddressInfo.Provider.Get()
                                                  .WhereEquals("AddressCustomerID", customer.CustomerID)
                                                  .TopN(1)
                                                  .FirstOrDefault();

    if (address != null)
    {
        // Updates the address properties
        address.AddressName = address.AddressName.ToLowerCSafe();

        // Saves the changes to the database
        AddressInfo.Provider.Set(address);
    }
}
> Back to list of examples

Updating multiple addresses

C#
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfo.Provider.Get()
                                            .WhereEquals("CustomerLastName", "Smith")
                                            .TopN(1)
                                            .FirstOrDefault();

if (customer != null)
{
    // Gets all addresses stored for the customer
    var addresses = AddressInfo.Provider.Get().WhereEquals("AddressCustomerID", customer.CustomerID);

    // Loops through the addresses
    foreach (AddressInfo modifyAddress in addresses)
    {
        // Updates the address properties
        modifyAddress.AddressName = modifyAddress.AddressName.ToUpperCSafe();

        // Saves the changes to the database
        AddressInfo.Provider.Set(modifyAddress);
    }
}
> Back to list of examples

Deleting an address

C#
// Gets the address
AddressInfo address = AddressInfo.Provider.Get()
                                              .WhereStartsWith("AddressName", "New")
                                              .TopN(1)
                                              .FirstOrDefault();

if (address != null)
{
    // Deletes the address
    AddressInfo.Provider.Delete(address);
}
> Back to list of examples

Customer newsletter subscriptions

Subscribing customers to newsletters

C#
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfo.Provider.Get()
                                            .WhereEquals("CustomerLastName", "Smith")
                                            .TopN(1)
                                            .FirstOrDefault();

// Gets the newsletter
NewsletterInfo newsletter = NewsletterInfo.Provider.Get("DancingGoatNewsletter", SiteContext.CurrentSiteID);

if ((customer != null) && (newsletter != null))
{
    // Prepares instances of the Xperience contact provider, email feed subscription service and contact relation assigner
    IContactProvider contactProvider = CMS.Core.Service.Resolve<IContactProvider>();
    ISubscriptionService subscriptionService = CMS.Core.Service.Resolve<ISubscriptionService>();
    IContactRelationAssigner contactRelationAssigner = CMS.Core.Service.Resolve<IContactRelationAssigner>();

    // Either gets an existing contact by email or creates a new contact object with the customer's email address and name
    ContactInfo customerContact = contactProvider.GetContactForSubscribing(customer.CustomerEmail, customer.CustomerFirstName, customer.CustomerLastName);

    // Subscribes the contact to the newsletter
    subscriptionService.Subscribe(customerContact, newsletter, new SubscribeSettings
    {
        AllowOptIn = true, // Allows double opt-in subscription for newsletters that have it enabled
        SendConfirmationEmail = true, // Allows sending of confirmation emails for the subscription

        // Removes the email address from the opt-out list for all marketing emails on the given site (if present)
        RemoveAlsoUnsubscriptionFromAllNewsletters = true,
    });

    // Links the customer with the contact
    contactRelationAssigner.Assign(
        customer.CustomerID,
        MemberTypeEnum.EcommerceCustomer,
        customerContact.ContactID);
}
> Back to list of examples

Unsubscribing customers from newsletters

C#
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfo.Provider.Get()
                                            .WhereEquals("CustomerLastName", "Smith")
                                            .TopN(1)
                                            .FirstOrDefault();

if (customer != null)
{
    // Prepares an instance of the Xperience email feed subscription service
    ISubscriptionService subscriptionService = CMS.Core.Service.Resolve<ISubscriptionService>();

    // Adds the customer to the marketing email opt-out list (for all email feeds)
    // Ensures that the customer no longer receives and marketing emails
    subscriptionService.UnsubscribeFromAllNewsletters(customer.CustomerEmail, null);
}
> Back to list of examples

Customer wishlists

Adding products to a wishlist

C#
// Gets the product
SKUInfo product = SKUInfo.Provider.Get()
                                   .WhereEquals("SKUName", "NewProduct")
                                   .TopN(1)
                                   .FirstOrDefault();

// Gets the first customer whose last name is 'Smith'
var customer = CustomerInfo.Provider.Get()
                                   .WhereEquals("CustomerLastName", "Smith")
                                   .TopN(1)
                                   .FirstOrDefault();

if ((customer != null) && (product != null))
{
    // Adds the product to the customer's wishlist
    WishlistItemInfo.Provider.Add(customer.CustomerUserID, product.SKUID, SiteContext.CurrentSiteID);
}
> Back to list of examples

Removing products from a wishlist

C#
// Gets the product
var product = SKUInfo.Provider.Get()
                               .WhereEquals("SKUName", "NewProduct")
                               .TopN(1)
                               .FirstOrDefault();

// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfo.Provider.Get()
                                            .WhereEquals("CustomerLastName", "Smith")
                                            .TopN(1)
                                            .FirstOrDefault();

if ((customer != null) && (product != null))
{
    // Gets the product from the wishlist
    WishlistItemInfo wishlistItem = WishlistItemInfo.Provider.Get(customer.CustomerUserID, product.SKUID, SiteContext.CurrentSiteID);

    if (wishlistItem != null)
    {
        // Removes the product from the wishlist
        WishlistItemInfo.Provider.Delete(wishlistItem);
    }
}
> Back to list of examples