Showing posts with label eCommerce. Show all posts
Showing posts with label eCommerce. Show all posts

Tuesday, March 15, 2016

EpiServer for beginners: How to add custom fields to Order (eCommerce)

Introduction

One of the simplest things that any developer would want is to add some fields to Order table or in related ones. When dealing with content pages, it would be handled automatically using Code-First methodology. But in the eCommerce system you have to do some steps.

Example

When you are dealing with external systems, you will need to have their Id in your system and since it is related to your order, well it is not a good idea to put in somewhere else.
Also, you may want to add external fields like status or etc to your order.

Steps

steps in Ecommerce

1- Go to Commerce Manager> Administration >Meta fields> New Meta Field



2- Fill name and friendly name and choose type of your field. Then choose properties like search option as you need. just have in mind that changing allow null field would be hard.

In these 2 steps, you've created a field in your order. Now you have to assign it to any table that you need.
3-  Go to meta classes right under order systems and select the main table(element) that you want to add your field. For instance, I want to add my field to my order form.

4- Now select the related external table related to that element. For order from it is only one, but it differs from element to element.
5- Select your field and click "OK"

Steps in your code

We have our field in our DB now.It is very easy to use it in your code.
The easiest way is to write a code like this line for setting the field:

Code:
 order.OrderForms[0]["MyFieldsName"] = value;

And Like this one to Getting it (for strings):

Code:
var x=order.OrderForms[0]["MyFieldsName"].ToString();

But It is always good to have a class, that contains names and methods for handling fields and their names.
Use Constants to avoid writing wrong names. Also set and get all fields in the same class so you have them centralized and use those methods.

Code:
 private const string MyFieldName = "MyFieldName";

public static string GetMyField()
{
   return order.OrderForms[0][MyFieldName].ToString();        
}

Edit:

I wrote a post in here  describing how to add the field grammatically.