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
CustomerInfo.Provider.Set(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
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);
Updating a customer
// 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);
}
Updating multiple customers
// 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);
}
Deleting customers
// 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);
}
Customer addresses
Creating an address
// 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);
}
Updating an address
// 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);
}
}
Updating multiple addresses
// 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);
}
}
Deleting an address
// Gets the address
AddressInfo address = AddressInfo.Provider.Get()
.WhereStartsWith("AddressName", "New")
.TopN(1)
.FirstOrDefault();
if (address != null)
{
// Deletes the address
AddressInfo.Provider.Delete(address);
}
Customer newsletter subscriptions
Subscribing customers to newsletters
// 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);
}
Unsubscribing customers from newsletters
// 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);
}
Customer wishlists
Adding products to a wishlist
// 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);
}
Removing products from a wishlist
// 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);
}
}