Extending the CustomerContact object in Episerver Commerce

Here’s a quick method for adding your own properties to the CustomerContact object:

public virtual bool AddCustomerContactMetaField(string name, string friendlyName, string type)
        {
            var customerContactMetaClass = DataContext.Current.GetMetaClass(ContactEntity.ClassName);

            if(customerContactMetaClass.Fields[name] != null)
            {
                return false;
            }
            
            return customerContactMetaClass.CreateMetaField(name, friendlyName, type, new Mediachase.BusinessFoundation.Data.Meta.Management.AttributeCollection()) == null;
        }

You can then assign and read values to and from your property like this:

CustomerContact contact = CustomerContact.CreateInstance();

// Assign value
contact.Properties.Add("MyProperty", "value");

// Reading a value
string value = contact.Properties.GetValue<string>("MyProperty");