How To Add A Custom Property To The Purchase Order in Episerver Commerce

I’ve talked previously about Adding a custom field into the commerce customer object via code, Adding Custom MetaData to the LineItem Class in Episerver Commerce via Code and Adding Custom MetaData to the Cart Class in Episerver Commerce via Code.

In today’s post, I’m going to talk about doing the same for the Purchase Order class.  After a customer has placed an order, you will need to use the Purchase Order class to save information about the order for invoicing, reports etc..

var name = "ProductName";
var metaNamespace = string.Empty;
var friendlyName = "ProductName";
var description = string.Empty;
var metaFieldType = MetaDataType.LongString;
var isNullable = true;
var length = 0;
var isMultiLanguage = false;
var isSearchable = false;
var isEncrypted = false;
var metaClass = OrderContext.Current.PurchaseOrderMetaClass;
if (metaClass.MetaFields.Any(x => x.Name == name))
return false;
var metaContext = OrderContext.MetaDataContext;
var metaField = MetaField.Create(metaContext,
metaNamespace,
name,
friendlyName,
description,
metaFieldType,
length,
isNullable,
isMultiLanguage,
isSearchable,
isEncrypted);
metaClass.AddField(metaField);

In order to to add a new meta field, we need to use the MetaDataPlus API.  First, we need to check the field doesn’t already exist.  If it does, the AddField method will throw an exception.  This is done using OrderContext.Current.PurchaseOrderMetaClass.  As the name suggests, this gives us access to the in-built PurchaseOrderMetaClass metadata class.

After we know the field doesn’t already exist we can then create it.  This is done by creating a meta field, then using the OrderContext.MetaDataContext in order to save it.  First we use MetaField.Create passing in the parameters we care about, then the result of that is passed into OrderContext.MetaDataContext.AddField() and the rest is done.  You should know using this approach, each field you add needs to be unique.  Even if you tried to add ProductName on the Line Item object, for example, Episerver will throw.

Conclusion

In today’s post we’ve talked about how to create custom fields in the Purchase Order class. This is done using the Metadata Plus API.

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

1 reply

Trackbacks & Pingbacks

  1. […] If the purchase order object does not have the fields you need, you can either add them in manually via commerce. Personally, I don’t like this approach.  Instead, I like to add new fields in code.  This way I can guarantee that no typos happen and I can clean my database and rebuild the database easily. If you need some more information about how to do this then I would recommend reading, How To Add A Custom Property To The Purchase Order in Episerver Commerce. […]

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 *