What's Commerce version you're working with?
I'm trying to reproduce your issue but found that IsGift was in LineItem already.
/Son Do
Hi @Son Do
I am using version 9.15.0.1605 of Mediachase.Commerce.
ANy custom meta property that i defined in TreasureLineItem throws null exception.
Cheers
I suppose you want to use the "Strongly typed" approach? Because otherwise you can just add metafields to LineItem and access them like lineItem["YourMetaField"].
The stacktrace of the exception would be helpful.
Regards,
/Q
Hi @Quan,
Yes, I am trying to use strongle typed approach. I think the mediachase library has some problem.
The call for adding code to cart AddEntry returns lineitem class for the first time.
e.g call
CartHelper ch = new CartHelper(name); var lineItem = ch.AddEntry(entry, cartLineItem.Quantity, false, new CartHelper[] { });
for e.g If the entry code is added for the first time then return type is always LineItem
If you re-add the same code again the return type comes out as TreasuresLineItem.
The reason for this i beleive that Medachase.Commerce uses different way to initialize the line item. I have been debugging and found out that In some places it uses
LineItem lineItem = new LineItem()
and in some places through orderForm it uses
(LineItem) OrderContext.Current.LineItemClassInfo.CreateInstance();
I think because of these descrepenceies, if given code is added to cart for the first time it returns LineItem and then TreasuresLineItem for the subsequent call for the given code.
Anyway, i still get the null reference exception if i try to access custom property in TreasuresLineItem. Unless i add custom LineItem Class with same name("TreasuresLineItem") and assign same custom property to LineItemEx and newly added CustomLineItem("TreasuresLineItem")
The drawback of this, It adds new tables LineItemEx_TreasuresLineItem and LineItemEx_TreasuresLineItem_Localization but still saves values in LineItemEx.
and if i try to replace ecf.order.config
<MetaClasses><LineItemClass name="TreasuresLineItem"> from LineItemEx in order to save new value into LineItemEx_TreasuresLineItem and LineItemEx_TreasuresLineItem_Localization then returned object from
ch.AddEntry(entry, cartLineItem.Quantity, false, new CartHelper[] { }) is always of LineItem.
Hi,
Can you post the stacktrace of the null reference exception you got?
/Q
Hi Quan,
I have decided not to inherit from LineItem class instead use in a way defined below
public class TreasuresLineItem { private readonly LineItem _lineItem; public LineItem LineItem { get { return _lineItem; } } public TreasuresLineItem(LineItem lineItem) : base() { _lineItem = lineItem; } public bool IsGift { get { return _lineItem.GetBool(MetaFieldsConstants.LineItem.IsGift); } set { _lineItem[MetaFieldsConstants.LineItem.IsGift] = value; } } public string Recipeints { get { return _lineItem.GetString(MetaFieldsConstants.LineItem.Recipients); } set { _lineItem[MetaFieldsConstants.LineItem.Recipients] = value; } } public string DeliveryInstructions { get { return _lineItem.GetString(MetaFieldsConstants.LineItem.DeliveryInstructions); } set { _lineItem[MetaFieldsConstants.LineItem.DeliveryInstructions] = value; } } public string Abstract { get { return _lineItem.GetString(MetaFieldsConstants.LineItem.LineItemAbstract); } set { _lineItem[MetaFieldsConstants.LineItem.LineItemAbstract] = value; } } public string ImageUrl { get { return _lineItem.GetString(MetaFieldsConstants.LineItem.ImageUrl); } set { _lineItem[MetaFieldsConstants.LineItem.ImageUrl] = value; } } public MetaDictionaryItem ProductType { get { return _lineItem.GetDictionaryItem(MetaFieldsConstants.LineItem.ProductType); } set { _lineItem[MetaFieldsConstants.LineItem.ProductType] = value; } } }
Hi,
I have custom line item with name TreasuresLineItem
I have below line in ecf.order.config
The class looks like below
When i try to add entry to cart and then cast it to TreasureLineItem. The system cast it successfully but accessing properties throws an null exception
/* Need entry for adding to cart */
var entry = CatalogContext.Current.GetCatalogEntry(cartLineItem.Code);
var lineItem = ch.AddEntry(entry, cartLineItem.Quantity, false, new CartHelper[] { }) as TreasuresLineItem;
Throws exception
lineItem.IsGif accessing property throws exception
I have checked meta field exists in database and it is linked with TreasureLineItem, The database table for extended line item with column also exists.
Do you have any idea why it is happening like this?