Adding Custom MetaData to the LineItem Class in Episerver Commerce via Code

Hey, folks. When we work with the basket in Episerver commerce it is quite likely you may need to store some custom data against a line item. This might be the image of the product, the discount applied to it etc… In commerce manager you can do this with the GUI, however, you can also add custom properties to the LineItem class via code. On a recent project, we had a requirement to pre-populate the whole Episerver and Episerver commerce database on first website load for the developers environment set-up. Having the ability to trash your database and re-build it can be a very useful for development, however, before you go running off and creating your own fixtures I will warn you there is a lot of overhead to create the code.

In this scenario, an extra property which will call “Custom Property” for shits and giggles was needed to be added onto the LineItem class. After a quick Google, I struggled to find any documentation on this but it can be done using the metadata plus API. Using this API and the OrderContext you can add as many custom properties as you want against LineItems, as shown:

using Mediachase.Commerce.Orders;
var lineItemMetaClass= OrderContext.Current.LineItemMetaClass;
var context = OrderContext.MetaDataContext;
var name = "CustomProperty";
var displayName = "Custom Property";
var length = 256;
var metaFieldType = MetaDataType.LongString;
var metaNamespace = string.Empty;
var description = string.Empty;
var isNullable = false;
var isMultiLanguage = true;
var isSearchable = true;
var isEncrypted = true;
var metaField = MetaField.Create(context ,
metaNamespace,
name,
displayName,
description,
metaFieldType,
length,
isNullable,
isMultiLanguage,
isSearchable,
isEncrypted);
lineItemMetaClass.AddField(metaField);

I’ve simplified this code snippet to make it easier to read. First we use OrderContext.Current.PurchaseOrderMetaClass to get the MetaClass. This is the class that will save our property. Next we use OrderContext.MetaDataContext to get the metadata context. After that, it’s simply using MetaField to create the property and then metaClass.AddField to save it.

You can then access your property using the following snippet:

var lineItem = new LineItem();
lineItem[CustomProperty] = "Setting";
var customProperty = lineItem[CustomProperty] as string;

Conclusion

In today’s guide, we’ve talked about how to extend the LineItem class. This can be done either via the commerce manager or code. When working with code you will need to look in the Mediachase.Commerce.Orders namespace for OrderContext.Current.PurchaseOrderMetaClass and OrderContext.MetaDataContext. Using MetaField.Create() you can then add in whatever properties you may need.

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 *