How to create a Customer record based on a User account

   —   

Due to significant required information difference between Customer and User accounts, Kentico does not offer their direct conversion. This article explains reasons why, and a possible way how to do the Conversion via API.

Since User and Customer accounts require different information, the only possibility of Conversion between the two is for the User to fill out additional information, and the only place where that can be done is during shopping cart checkout.

The following method will verify the user with specified UserID does not already have an existing Customer record and then create one. Based on the sensible parameter, it will cancel the Customer creation if First or Last name could not be determined and the user Email is also empty, so as not to create accounts difficult to identify in CMS Desk.

public bool ConvertUserToCustomer(int userID, bool sensible) { if (userID < 0) return false; UserInfo user = UserInfoProvider.GetUserInfo(userID); CustomerInfo ci = CustomerInfoProvider.GetCustomerInfoByUserID(userID); if (user == null || ci != null) return false; // Create customer object ci = new CustomerInfo(); ci.CustomerFirstName = user.FirstName; ci.CustomerLastName = user.LastName; string[] splitFullName = user.FullName.Split(' '); // If first or last name is empty, try to parse it from FullName if (string.IsNullOrEmpty(ci.CustomerFirstName) && splitFullName.Length > 0) ci.CustomerFirstName = splitFullName[0]; if (string.IsNullOrEmpty(ci.CustomerLastName) && splitFullName.Length > 1) ci.CustomerLastName = splitFullName[splitFullName.Length - 1]; // Save email. ci.CustomerEmail = user.Email; // Verify that either First and Last name or Email has been supplied. if (sensible && ((string.IsNullOrEmpty(ci.CustomerFirstName) || string.IsNullOrEmpty(ci.CustomerLastName)) && string.IsNullOrEmpty(ci.CustomerEmail))) return false; // Save any other known information ci.CustomerPhone = Convert.ToString(user.UserSettings.GetValue("UserPhone")); // No other information is stored with user account, leave it blank. ci.CustomerCompany = ""; ci.CustomerOrganizationID = ""; ci.CustomerTaxRegistrationID = ""; ci.CustomerUserID = user.UserID; ci.CustomerSiteID = 0; ci.CustomerEnabled = true; ci.CustomerCreated = DateTime.Now; // Save user info in the database CustomerInfoProvider.SetCustomerInfo(ci); return true; }

Kentico however does not require those fields for the Customer record to be valid. If you plan on working with the Customer record only with relation to the User account via API, for example to assign a discount level, you can turn the sensibility check off.

-jd-

 


Applies to: Kentico CMS 7.x

 

Share this article on   LinkedIn Google+

Josef Dvorak

Hello, I am a Solution Architect with Kentico. My primary focus is the E-Commerce module, however it is not uncommon for me to help with other modules as well.