Create the custom fields for the CartOrder and Order classes

In order to store the values entered in the UI fields that you have created in the previous step, you have to create custom fields for the CartOrder and Order objects. During the checkout process a CartOrder object is used to gather the information and preserve it in the database.

NOTE: Ecommerce order data is stored in multiple tables. During the checkout process this data is stored in “Cart” tables. These tables act like a temporary buffer to hold all of the information that a customer will enter during the multiple step check out process (shipping address, billing address, selected shipping method, payment method, coupon codes, etc.). Once a customer chooses to actually purchase the items in their cart, the contents of the “Cart” tables are then copied to the similarly named “Order” tables.

Because you will only want to attempt to modify the database structure once, a good place to perform this action is in the Application_Start event handler in the Global.asax file. By default, a new SitefinityWebApp project does not include this file so you will need to add a new Global.asax file to your project. In the Application_Start handler, hook up to the Bootstrapped event of the Bootstrapper class. This event occurs when the first instance of your Sitefinity CMS site is requested.

To create the fields, you will be using a method named CreateCustomOrderFields(). This method must perform the following:

  1. Check whether the Order and OrderCart classes are configured to support meta types. If not you have to create a new meta type for each of the classes.
  2. Create the required custom fields for each of the classes.

To create the custom fields for the CartOrder and Order objects, you must perform the following.

  1. Create the CreateCustomOrderFields() method.
    In the Global.asax file, create a method with this definition: private void CreateCustomOrderFields().
  2. Implement the logic for creating the custom fields.
    To do this, you must perform the following:
    1. Get the metadata manager.
      Get an instance of the MetadataManager object.
    2. Create a meta type for the Order class.
      If the Order class does not have a meta type associated to it, create one. To create a meta type, call the CreateMetaType method of the metadata manager and pass the type of the class. After this save the changes to the metadata manager.
    3. Create the dynamic fields for the Order class.
      To create the dynamic fields, you can use the Fluent API. First get the dynamic data facade. Specify the dynamic type. Then, get the Field singular facade. Create a new dynamic field using the TryCreateNew method. As arguments pass the name and the type of the field. In this example, both fields will be of type string. Finally, save the changes to the manager specifying it to upgrade the database.
    4. Repeat steps from 2 to 3 for the CartOrder class.
      Do the same things, to create the same fields for the CartOrder class.
    The key point to notice in this step is that you are creating new meta fields for both the Order class and the CartOrder class. Now whenever a CartOrder or Order object is persisted to the database, additional fields with the corresponding names will appear in the respective database tables (“sf_ec_cart_order” and “sf_ec_order”).
  3. Call the CreateCustomOrderFields method.
    In the event handler for the Initialized event, call the CreateCustomOrderFields method.

Use the following code sample:

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Get started with Integration Hub | Sitefinity Cloud | Sitefinity SaaS

This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.

Web Security for Sitefinity Administrators

This free lesson teaches administrators the basics about protecting yor Sitefinity instance and its sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?