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.

14 comments:

  1. It will be a run time error for order.OrderForms[0][MyFieldName].ToString(), if order.OrderForms[0][MyFieldName] is null

    ReplyDelete
    Replies
    1. Of course :) it was a simple code without try/catch, type changes and etc.

      Delete
    2. rather to use try/catch, Convert.ToString(order.OrderForms[0][MyFieldName]) may work

      Delete
  2. Meta fields can be created pragmatically

    ReplyDelete
    Replies
    1. Cool! Can you share the article?

      Delete
    2. Find it on world

      By: Quan Mai November 17 2014, 4:06Content is edited
      Hi,Hopefully this is not too late for you. But's here the pseudo code you can use:var mc= DataContext.Current.MetaModel.MetaClasses["Address"];using (MetaFieldBuilder builder = new MetaFieldBuilder(mc)){ builder.CreateText("NewField", "{Customer:Address_mf_NewField}", true, 64, false);builder.SaveChanges();}
      Regards./Q

      Delete
    3. @Khurram Khan: I think you pasted the wrong code. The code you showed is for the BusinessFoundation, but the MetaClass/MetaField for Order is in Mediachase.MetaDataPlus.

      Yeah I know it's confusing :)

      Delete
  3. Good blog post. Here is an example on how to add meta fields during site startup:
    https://github.com/BVNetwork/CommerceStarterKit/blob/master/src/web/Business/Initialization/MetadataInitialization.cs

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. This comment has been removed by a blog administrator.

    ReplyDelete
  7. cheap christian louboutin shoes, combining elegant style and cutting-edge technology, a variety of styles of cheap louboutin boat shoe, the pointer walks between your exclusive taste style.

    ReplyDelete