London Dev Meetup Rescheduled! Due to unavoidable reasons, the event has been moved to 21st May. Speakers remain the same—any changes will be communicated. Seats are limited—register here to secure your spot!

[11.8.5] Custom LineItem implementation for serializedcart?

Vote:
 

Hi,

Does anyone have any hints about the steps needed to implement a custom LineItem (for serialized carts)?

I would like to have a custom LineItem class, with properties for various things (to make it simpler to use than using ...Properties[".."] as string, etc.), what needs to be changed/coded to make ICart.CreateLineItem() return an instance of my custom LineItem class, etc?

I tried inheriting from the Mediachase.Commerce.Orders.LineItem class, and registering the new class in ecf.order.config, but I get an exception about EPiServer.Commerce.Order.Internal.SerializableLineItem not being castable to my custom line item class.

#195072
Jul 12, 2018 14:56
Vote:
 

SerializableCart is different so the settings in ecf.order.config do not apply. I haven't tried it but I think you would have to extend SerializableLineItem (yes, it is supposed an internal class) and read/write to Properties from your properites.

#195095
Jul 13, 2018 10:01
Vote:
 

I think think it would be "safer" in the long run to just go ahead and create extension methods for ILineItem (ore maybe IExtendedProperties) and not be dependent on a specific implementation.

Thank you (again) for you help! :)

#195144
Jul 16, 2018 10:23
Vote:
 

The biggest problem with the extension methods are that they are harder to test.

You can do something like this:

		public static void SetProperty(Hashtable properties, string name, object value)
		{
			properties[name.ToLower()] = value;
		}

Then you would be able to call it on any lineitem to save the value:

SetLineItemProperty(lineItem, nameof(CartItemModel.IsDirectDeliveryProduct), model.IsDirectDeliveryProduct);

Be careful with arrays though, since they are not supported by Episerver.

#195147
Jul 16, 2018 12:29
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.