bannerepitutorial

Episerver Commerce : How To Add Custom Properties to the Customers Objects in code

When you use commerce after someone signs-up using the membership provider, you may want to create your own commerce user to store custom data.  For example, on one project when you signed up, you had to supply your date of birth and a newsletter opt-in.  As this is profile information rather than authentication data, a membership provider doesn’t provide the methods to update profile information, and nor should it.

Instead, one solution is to stop everything you need in as a commerce customer.  To get started saving your customers details into commerce, you need to call CustomerContact.CreateInstance(membershipUser).  If you don’t do this and try to manually create a new customer, Epi will create two commerce customers which you definitely don’t want.

In this project we wanted to unit test a lot of the customer and authentication code, so to make this unit testable, I have wrapped the code into a CustomerContactHelper with an interface so it can be swapped out in a test library, if required.

public class CustomerContactHelper : ICustomerContactHelper
{
private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public bool SaveChanges(CustomerContact customerContact)
{
var result = false;
try
{
result = customerContact.SaveChanges() != null;
}
catch (ActivationException ex)
{
Logger.Error(ex);
}
return result;
}
public void UpdateBusinessManager(CustomerContact customerContact)
{
BusinessManager.Update(customerContact);
}
public CustomerContact CreateInstance(MembershipUser membershipUser)
{
return CustomerContact.CreateInstance(membershipUser);
}
}
public interface ICustomerContactHelper
{
bool SaveChanges(CustomerContact customerContact);
void UpdateBusinessManager(CustomerContact customerContact);
CustomerContact CreateInstance(System.Web.Security.MembershipUser membershipUser);
}

After this, you need to update your data into the customer object.  Out of the box commerce comes with a number of default properties like, First Name, Last Name but, in my circumstance, there was no default Newsletter opt-in.  Adding a property doesn’t involve that much code luckily.  I wrote an article how to do this called Adding a custom field into the Commerce Customer Object via code

After you have added all the data you require into your commerce customer, you then need to call customerContact.SaveChanges() method to persist it back to the database. Again, I have wrapped the save method in the call above so we can unit test the code in question.  If you try to call SaveChanges() out of a commerce context you will get an ActivationException so I had to wrap it in a try/catch.

That’s it! You can now create your own commerce customers via code!

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *