Try our conversational search powered by Generative AI!

Loading...
Applies to versions: 10 and higher
Other versions:
ARCHIVED This content is retired and no longer maintained. See the version selector for other versions of this topic.

Extending order classes

Recommended reading 
Note: This documentation is for the preview version of the upcoming release of CMS 12/Commerce 14/Search & Navigation 14. Features included here might not be complete, and might be changed before becoming available in the public release. This documentation is provided for evaluation purposes only.

This topic explains how to extend order classes in Optimizely Commerce. The order system default implementation contains many classes that you can extend with your own fields. This means that you can customize fields to all order objects to help with your business use cases.

Classes in this topic are available in the Mediachase.Commerce.Orders namespace. The order classes that you can extend are:

  • Cart
  • PurchaseOrder
  • PaymentPlan
  • OrderForm
  • Payment
  • Shipment
  • LineItem
  • OrderGroupAddressType

Configuring a meta-class

    Working with metadata types

    The default implementation uses Mediachase.MetaDataPlus for its storage of extended attributes.  The list of available types for use in IExtendedProperties.Properties are represented in Mediachase.MetaDataPlus.Configurator.MetaDataType.  

    Supported Types
    MetaDataType .NET Type
    MetaDataType.DateTime DateTime
    MetaDataType.Date DateTime
    MetaDataType.DictionarySingleValue MetaDictionaryItem
    MetaDataType.EnumSingleValue MetaDictionaryItem
    MetaDataType.Float double
    MetaDataType.Decimal decimal
    MetaDataType.Money decimal
    MetaDataType.Integer int
    MetaDataType.DictionaryMultiValue MetaDictionaryItem[]
    MetaDataType.EnumMultiValue MetaDictionaryItem[]
    MetaDataType.StringDictionary MetaStringDictionary
    MetaDataType.Boolean bool
    boolMetaDataType.Email string
    MetaDataType.URL string
    MetaDataType.ShortString string
    MetaDataType.LongString string
    MetaDataType.LongHtmlString string
    MetaDataType.File MetaFile
    MetaDataType.ImageFile MetaFile

    Accessing order metadata

    When determining if an object has extended  properties, see if the interface derives from EPiServer.Commerce.Storage.IExtendedProperties.

    var orderRepository = ServiceLocator.Current.GetInstance<IOrderRepository>();
    var contactId = PrincipalInfo.CurrentPrincipal.GetContactId();
    var cart = orderRepository.LoadCart<ICart>(contactId, "Default");
    
    var cartField = cart.Propeties["myfield"].ToString();
    var formField = cart.GetFirstForm().Properties["myFormField"].ToString();
    var paymentField = cart.GetFirstForm().Payments.First().Properties["myPaymentField"].ToString();
    var shipmentField = cart.GetFirstShipment().Properties["myShipmentField"].ToString();
    var lineItemField = cart.GetAllLineItems().First().Properties["myLineItemField"].ToString();

    Setting order metadata

    When determining if an object has extended properties, see if the interface derives from EPiServer.Commerce.Storage.IExtendedProperties.

    var orderRepository = ServiceLocator.Current.GetInstance<IOrderRepository>();
    var contactId = PrincipalInfo.CurrentPrincipal.GetContactId();
    var cart = orderRepository.LoadCart<ICart>(contactId, "Default");
    
    cart.Propeties["myfield"] = "hello";
    cart.GetFirstForm().Properties["myFormField"] = 23m;
    cart.GetFirstForm().Payments.First().Properties["myPaymentField"] = 11;
    cart.GetFirstShipment().Properties["myShipmentField"] = 9;
    cart.GetAllLineItems().First().Properties["myLineItemField"] = "yes";

    Accessing order metadata [Legacy]

    To access order meta-objects, each order meta-class has a dictionary of meta-fields accessible by the root object.

    Example: accessing meta-fields from different order meta-fields

    Cart newCart = OrderContext.Current.GetCart("myname", newCustomer);
    //This is the meta field data access
    string myCartField = newCart["myField"].ToString();
    string orderformField = newCart.OrderForms[0]["myOrderFormField"].ToString();
    string paymentField = newCart.OrderForms[0].Payments[0]["myPaymentField"].ToString();
    string shipmentField = newCart.OrderForms[0].Shipments[0]["myShipmentField"].ToString();
    Do you find this information helpful? Please log in to provide feedback.

    Last updated: Jul 02, 2021

    Recommended reading