Created
July 2, 2024 21:34
-
-
Save paulmcgann/92a9a18ff2ca51061aa9a6729a468f2c to your computer and use it in GitHub Desktop.
Extend LineItems Cart Object Optimizely - Property
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Serializable] | |
public class CustomLineItem : LineItem | |
{ | |
public CustomLineItem() : base() { } | |
protected CustomLineItem(SerializationInfo info, StreamingContext context) : base(info, context) | |
{ | |
NewProperty = info.GetString("NewProperty"); | |
} | |
public string NewProperty { get; set; } | |
public override void GetObjectData(SerializationInfo info, StreamingContext context) | |
{ | |
base.GetObjectData(info, context); | |
info.AddValue("NewProperty", NewProperty); | |
} | |
} | |
public class CartHelper | |
{ | |
private readonly IOrderRepository _orderRepository; | |
public CartHelper(IOrderRepository orderRepository) | |
{ | |
_orderRepository = orderRepository; | |
} | |
public void SaveCart(ICart cart) | |
{ | |
if (cart == null) throw new ArgumentNullException(nameof(cart)); | |
using (var stream = new MemoryStream()) | |
{ | |
var formatter = new BinaryFormatter(); | |
formatter.Serialize(stream, cart); | |
var serializedCart = stream.ToArray(); | |
// Save serialized cart to persistent storage | |
File.WriteAllBytes("cart.dat", serializedCart); // Example using a file | |
} | |
} | |
public ICart LoadCart() | |
{ | |
// Retrieve the serialized cart from persistent storage | |
var serializedCart = File.ReadAllBytes("cart.dat"); // Example using a file | |
using (var stream = new MemoryStream(serializedCart)) | |
{ | |
var formatter = new BinaryFormatter(); | |
var cart = formatter.Deserialize(stream) as ICart; | |
return cart; | |
} | |
} | |
} | |
public class CartService | |
{ | |
private readonly IOrderRepository _orderRepository; | |
private readonly IOrderGroupFactory _orderGroupFactory; | |
private readonly CartHelper _cartHelper; | |
public CartService(IOrderRepository orderRepository, IOrderGroupFactory orderGroupFactory) | |
{ | |
_orderRepository = orderRepository; | |
_orderGroupFactory = orderGroupFactory; | |
_cartHelper = new CartHelper(orderRepository); | |
} | |
public void AddCustomLineItemToCart() | |
{ | |
var cart = _orderRepository.LoadCart<ICart>(CustomerContext.Current.CurrentContactId, "Default") ?? _orderGroupFactory.CreateCart<CustomerContext.Current.CurrentContactId>("Default"); | |
var customLineItem = new CustomLineItem | |
{ | |
Code = "TestProduct", | |
Quantity = 1, | |
NewProperty = "CustomValue" | |
}; | |
cart.GetFirstShipment().LineItems.Add(customLineItem); | |
_orderRepository.Save(cart); | |
} | |
public void ExampleUsage() | |
{ | |
AddCustomLineItemToCart(); | |
var cart = _orderRepository.LoadCart<ICart>(CustomerContext.Current.CurrentContactId, "Default"); | |
if (cart != null) | |
{ | |
_cartHelper.SaveCart(cart); | |
} | |
var loadedCart = _cartHelper.LoadCart() as ICart; | |
if (loadedCart != null) | |
{ | |
var customLineItem = loadedCart.GetFirstShipment().LineItems[0] as CustomLineItem; | |
if (customLineItem != null) | |
{ | |
string value = customLineItem.NewProperty; | |
Console.WriteLine("Custom Line Item Property Value: " + value); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment