November Happy Hour will be moved to Thursday December 5th.
November Happy Hour will be moved to Thursday December 5th.
Just to touch base: Has your LineItem MetaClass been extended with the "Test" metafield?
The LineItem MetaClass has not been altered - loving the serialized cart where that is not required
Ok. That's probably the issue then :D
PurchaseOrders aren't serialized so you need to extend the classes anyway if you intend to convert serializable carts to purchase orders. It's described further here:
https://world.episerver.com/documentation/developer-guides/commerce/orders/serializable-carts/#Serializablecomponents (Under "Adding custom data" -> Note 1 & Note 2)
Try extending the LineItem MetaClass and see if that works.
Thanks for quick diagnostic!
Is it posible to alter the LineItem MetaClass from an InitializationModule or the like? - I would hate to do it throgh the UI
Because you ask "is it" - so the answer is yes.
You will need to load the MetaClass (MetaClass.Load) and check if it has the metafield or not (via metaClass.MetaFields), if it not, then create one (MetaField.Create), then add it to the MetaClass (MetaClass.AddField)
I wrote about it somewhere ...
It's possible yes, I haven't done it specifically through an Initializable module but I have example code for creating the actual field:
// For working with metafields I like to have their names as constants somewhere so there won't be any issues when calling for it. This is just an example: public class MyLineItemMetaFieldConstants { public const string Test = "LineItemTest"; // usage would be LineItem.Properties[MyLineItemMetaFieldConstants.Test] } // Somewhere you'll add this public static void AddTestLineItemMetaField() { var metaClass = MetaClass.Load(CatalogContext.MetaDataContext, "LineItemEx"); var currentFields = metaClass.GetUserMetaFields().ToList(); var metaField = MetaField.Load(CatalogContext.MetaDataContext, MyLineItemMetaFieldConstants.Test) ?? MetaField.Create( CatalogContext.MetaDataContext, "Mediachase.Commerce.Orders.User.LineItem", // MetaNamespace (should be lineitem in your case) MyLineItemMetaFieldConstants.Test, // name MyLineItemMetaFieldConstants.Test, // FriendlyName "Test field for testing purposes", // Description MetaDataType.ShortString, // Database type 512, // Databae length true, // nullable false, // multi language value false, // allow search false); // is encrypted if (currentFields.All(x => x.Name != MyLineItemMetaFieldConstants.Test)) metaClass.AddField(metaField); }
Edit: Oops, Quan had already answered. But oh well here's code example anyway. :D
Thanks a bunch - it works like a charm*
I ended up with this
[ModuleDependency(typeof(InitializationModule))] public class OrderSystemInitializationModule : IConfigurableModule { public void Initialize(InitializationEngine context) { // if we AddCustomPropertiesToLineItem(); } public void Uninitialize(InitializationEngine context) { } public void ConfigureContainer(ServiceConfigurationContext context) { } private void AddCustomPropertiesToLineItem() { var fieldExists = MetaField.Load(CatalogContext.MetaDataContext, LineItemExtensions.VariantIdKey) != null; if (!fieldExists) { var newMetaField = MetaField.Create( CatalogContext.MetaDataContext, "Mediachase.Commerce.Orders.User.LineItem", // MetaNamespace LineItemExtensions.VariantIdKey, // name LineItemExtensions.VariantIdKey, // FriendlyName "Holds the SapId of the line item", // Description MetaDataType.ShortString, // Database type 50, // Databae length true, // nullable false, // multi language value false, // allow search false); // is encrypted var metaClass = MetaClass.Load(CatalogContext.MetaDataContext, "LineItemEx"); var currentFields = metaClass.GetUserMetaFields().ToList(); if (currentFields.All(x => x.Name != LineItemExtensions.VariantIdKey)) metaClass.AddField(newMetaField); } } }
*One small gotcha
It only works if the name og the meta field starts with a lower cased letter :-(
"test" works, but "Test" doesn't
[Pasting files is not allowed]
I haven't experienced that. You should be able to have "Test" as a name. What error do you get?
Btw minor performance thing in your code, if the metaField != null you can just return right there and skip the step of loading the MetaClass. :D
I get not error, just en empty property :-(
True about the perf thing - updated code above
Episerver: 11.8.0
Commerce: 12.2.0
I know this should have been fixed: https://world.episerver.com/support/Bug-list/bug/COM-4014 but I can't get it to work
Steps to reproduce
Any hints?