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 Field2- 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.
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(); }
I wrote a post in here describing how to add the field grammatically.
It will be a run time error for order.OrderForms[0][MyFieldName].ToString(), if order.OrderForms[0][MyFieldName] is null
ReplyDeleteOf course :) it was a simple code without try/catch, type changes and etc.
Deleterather to use try/catch, Convert.ToString(order.OrderForms[0][MyFieldName]) may work
DeleteMeta fields can be created pragmatically
ReplyDeleteCool! Can you share the article?
DeleteFind it on world
DeleteBy: 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
@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.
DeleteYeah I know it's confusing :)
Thanks guys :)
DeleteVery nice
Good blog post. Here is an example on how to add meta fields during site startup:
ReplyDeletehttps://github.com/BVNetwork/CommerceStarterKit/blob/master/src/web/Business/Initialization/MetadataInitialization.cs
Cool! Thanks Steve :) :+1:
DeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeletecheap 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