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:
- 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.
- 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.
- Create the CreateCustomOrderFields() method.
In the Global.asax file, create a method with this definition: private void CreateCustomOrderFields().
- Implement the logic for creating the custom fields.
To do this, you must perform the following:
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”).
- Get the metadata manager.
Get an instance of the MetadataManager object.
- 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.
- 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.
- Repeat steps from 2 to 3 for the CartOrder class.
Do the same things, to create the same fields for the CartOrder class.
- Call the CreateCustomOrderFields method.
In the event handler for the Initialized event, call the CreateCustomOrderFields method.
Use the following code sample: