Developer forum

Forum » Development » Set value in a orderlinefield in EcomCartLineAddedObserver

Set value in a orderlinefield in EcomCartLineAddedObserver

Mikkel Høst
Reply

Hi guys.

I have made some code in a observer (Dynamicweb.Extensibility.Subscribe(Dynamicweb.Notifications.eCommerce.Cart.Line.Added)

I have succefully added a product to the cart, but i need to set a tag / value so the frontend can render information about why we added this extra product to the cart. My idea was to use a custom OrderLineField and set the value here, but in this event i can see any orderline fields. I have created the field in the backend. 

DW 8.4.0.9.

Anyone know how i can do this?

 


Replies

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Hi Mikkel,

Did you attach the OrderLineField to the given product group? If not, then you need to do that first.

On the "Edit group" page, click the down arrow on "Order line fields" in the Ribbon and select your field. You should now be able to manipulate the field from your subscriber.

- Jeppe

 
Mikkel Høst
Reply

Hi Jeppe.

You are right! This was the problem. Thank you so much. But with this info i have some suggestions and a question.

1. When i create a new orderline like this

var ol = added.Cart.CreateOrderLine(product);

The ol doesn't have the orderlinefield (the product added is from at group that has the orderlin) It would be nice if it had.

2. The orderlinefields inherit from their parent groups, but in my case all my groups are parent, so if i could set the orderlinefields on the shop, that would be "better".

 

 

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply
This post has been marked as an answer

You are correct. It's not set automatically -- I'll look into getting it into the API. In the meantime, you should be able to work around this issue by doing something like this:

var values = new OrderLineFieldValueCollection();
foreach (var f in product.OrderLineFields)
{
	if (f.SystemName == "MySystemName")
	{
		values.Add(new OrderLineFieldValue(f.SystemName, "MyValue"));
	}
	else
	{
		values.Add(new OrderLineFieldValue(f.SystemName, string.Empty));
	}
}
ol.OrderLineFieldValues = values;

- Jeppe

Votes for this answer: 1
 
Mikkel Høst
Reply

Arrh okay, so i can just create it on the fly. Cool thanks. I enede up doing this

var newOl = added.Cart.CreateOrderLine(p);
newOl.OrderLineFieldValues.Add(new OrderLineFieldValue("MySystemName", "1"));

 

You must be logged in to post in the forum