In this tutorial, you will learn how to add a custom property to the out-of-the-box Purchase Order object in Episerver commerce. When building an e-commerce store in Episerver, it will be very important to customise the objects to suit your needs. After a customer has placed an order, you will need to use the PurchaseOrder class to save information about the order. If you want to store custom data for invoicing or reporting within Episerver Commerce, your best option is to add new fields using code. To add a new field you will need to use the MetaDataPlus API. In this guide, you will learn everything you need to do that 🔥🔥🔥

In Episerver Commerce, it is also possible to add custom properties onto any of the out-of-the-box objects. If you want to know how to do that you can read these tutorials:

The code to add a custom property is shown below:

To access the API to update the PurchaseOrder object, you will use OrderContext.Current. Specifically, you will use the PurchaseOrderMetaClass API. You will need to use the AddField() method, as can be seen on Line30, to define a new field.

Before creating a new field on the PurchaseOrder object using AddField(), you need to check the property doesn't already exist. If it does, AddField() will throw an exception.

After we know the field doesn't already exist we can then create it. This is done by creating a metafield, using the OrderContext.MetaDataContext.Create (as seen on Line18). You can configure the fields details by passing in data within the method's arguments. Next, you use OrderContext.MetaDataContext.AddField() to add the field to the object. You then persist it using Save() to persist the object.


You now know how to use the MetadataPlus API to add custom fields onto the PurchaseOrder class. Happy Coding 🤘