Customers


List of examples:

Customers

Creating an anonymous customer




// 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
CustomerInfoProvider.SetCustomerInfo(newCustomer);


> Back to list of examples

Creating a registered customer




// 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
UserInfoProvider.SetUserInfo(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
CustomerInfoProvider.SetCustomerInfo(newCustomer);


> Back to list of examples

Updating a customer




// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfoProvider.GetCustomers()
                                                .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
    CustomerInfoProvider.SetCustomerInfo(customer);
}


> Back to list of examples

Updating multiple customers




// Gets all customers whose last name starts with 'Sm'
var customers = CustomerInfoProvider.GetCustomers().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
    CustomerInfoProvider.SetCustomerInfo(modifyCustomer);
}


> Back to list of examples

Deleting customers




// Gets all customers whose last name starts with 'Sm'
var customers = CustomerInfoProvider.GetCustomers().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)
    {
        UserInfoProvider.DeleteUser(user);
    }

    // Deletes the customer
    CustomerInfoProvider.DeleteCustomerInfo(deleteCustomer);
}


> Back to list of examples

Customer addresses

Creating an address




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

// Gets a country for the address
CountryInfo country = CountryInfoProvider.GetCountryInfo("USA");

// Gets a state for the address
StateInfo state = StateInfoProvider.GetStateInfo("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
    AddressInfoProvider.SetAddressInfo(newAddress);
}


> Back to list of examples

Updating an address




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

if (customer != null)
{
    // Gets the first enabled address stored for the customer
    AddressInfo address = AddressInfoProvider.GetAddresses()
                                                  .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
        AddressInfoProvider.SetAddressInfo(address);
    }
}


> Back to list of examples

Updating multiple addresses




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

if (customer != null)
{
    // Gets all addresses stored for the customer
    var addresses = AddressInfoProvider.GetAddresses().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
        AddressInfoProvider.SetAddressInfo(modifyAddress);
    }
}


> Back to list of examples

Deleting an address




// Gets the address
AddressInfo address = AddressInfoProvider.GetAddresses()
                                              .WhereStartsWith("AddressName", "New")
                                              .TopN(1)
                                              .FirstOrDefault();

if (address != null)
{
    // Deletes the address
    AddressInfoProvider.DeleteAddressInfo(address);
}


> Back to list of examples

Customer newsletter subscriptions

Subscribing customers to newsletters




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

// Gets the newsletter
NewsletterInfo newsletter = NewsletterInfoProvider.GetNewsletterInfo("DancingGoatNewsletter", SiteContext.CurrentSiteID);

if ((customer != null) && (newsletter != null))
{
    // Prepares instances of the Kentico 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




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

if (customer != null)
{
    // Prepares an instance of the Kentico 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, sendConfirmationEmail : true);
}


> Back to list of examples

Customer wishlists

Adding products to a wishlist




// Gets the product
SKUInfo product = SKUInfoProvider.GetSKUs()
                                   .WhereEquals("SKUName", "NewProduct")
                                   .TopN(1)
                                   .FirstOrDefault();

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

if ((customer != null) && (product != null))
{
    // Adds the product to the customer's wishlist
    WishlistItemInfoProvider.AddSKUToWishlist(customer.CustomerUserID, product.SKUID, SiteContext.CurrentSiteID);
}


> Back to list of examples

Removing products from a wishlist




// Gets the product
var product = SKUInfoProvider.GetSKUs()
                               .WhereEquals("SKUName", "NewProduct")
                               .TopN(1)
                               .FirstOrDefault();

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

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

    if (wishlistItem != null)
    {
        // Removes the product from the wishlist
        WishlistItemInfoProvider.DeleteWishlistItemInfo(wishlistItem);
    }
}


> Back to list of examples