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);
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);
Updating a customer
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfoProvider.GetCustomers()
.WhereEquals("CustomerLastName", "Smith")
.FirstObject;
if (customer != null)
{
// Updates the customer's properties
customer.CustomerLastName = customer.CustomerLastName.ToLowerCSafe();
// Saves the changes to the database
CustomerInfoProvider.SetCustomerInfo(customer);
}
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);
}
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);
}
Customer addresses
Creating an address
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfoProvider.GetCustomers()
.WhereEquals("CustomerLastName", "Smith")
.FirstObject;
// 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);
}
Updating an address
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfoProvider.GetCustomers()
.WhereEquals("CustomerLastName", "Smith")
.FirstObject;
if (customer != null)
{
// Gets the first enabled address stored for the customer
AddressInfo address = AddressInfoProvider.GetAddresses()
.WhereEquals("AddressCustomerID", customer.CustomerID)
.FirstObject;
if (address != null)
{
// Updates the address properties
address.AddressName = address.AddressName.ToLowerCSafe();
// Saves the changes to the database
AddressInfoProvider.SetAddressInfo(address);
}
}
Updating multiple addresses
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfoProvider.GetCustomers()
.WhereEquals("CustomerLastName", "Smith")
.FirstObject;
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);
}
}
Deleting an address
// Gets the address
AddressInfo address = AddressInfoProvider.GetAddresses()
.WhereStartsWith("AddressName", "New")
.FirstObject;
if (address != null)
{
// Deletes the address
AddressInfoProvider.DeleteAddressInfo(address);
}
Customer newsletter subscriptions
Subscribing customers to newsletters
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfoProvider.GetCustomers()
.WhereEquals("CustomerLastName", "Smith")
.FirstObject;
// 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<IContactProvider>.Entry();
ISubscriptionService subscriptionService = CMS.Core.Service<ISubscriptionService>.Entry();
IContactRelationAssigner contactRelationAssigner = CMS.Core.Service<IContactRelationAssigner>.Entry();
// 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,
false);
}
Unsubscribing customers from newsletters
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfoProvider.GetCustomers()
.WhereEquals("CustomerLastName", "Smith")
.FirstObject;
if (customer != null)
{
// Prepares an instance of the Kentico email feed subscription service
ISubscriptionService subscriptionService = CMS.Core.Service<ISubscriptionService>.Entry();
// 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);
}
Customer wishlists
Adding products to a wishlist
// Gets the product
SKUInfo product = SKUInfoProvider.GetSKUs()
.WhereEquals("SKUName", "NewProduct")
.FirstObject;
// Gets the first customer whose last name is 'Smith'
var customer = CustomerInfoProvider.GetCustomers()
.WhereEquals("CustomerLastName", "Smith")
.FirstObject;
if ((customer != null) && (product != null))
{
// Adds the product to the customer's wishlist
WishlistItemInfoProvider.AddSKUToWishlist(customer.CustomerUserID, product.SKUID, SiteContext.CurrentSiteID);
}
Removing products from a wishlist
// Gets the product
var product = SKUInfoProvider.GetSKUs()
.WhereEquals("SKUName", "NewProduct")
.FirstObject;
// Gets the first customer whose last name is 'Smith'
CustomerInfo customer = CustomerInfoProvider.GetCustomers()
.WhereEquals("CustomerLastName", "Smith")
.FirstObject;
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);
}
}