Developer forum

Forum » Development » LineAdded not called when adding to cart

LineAdded not called when adding to cart

Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi there,

I have a solution that contains code like this:

OrderLineViewModel cartItem =  new OrderLineViewModel();
cartItem.ProductId = prodId;
cartItem.ProductLanguageId = Dynamicweb.Ecommerce.Common.Context.LanguageID;
cartItem.ProductVariantId = "";
cartItem.Quantity = Dynamicweb.Core.Converter.ToDouble(productQuantity);
cartItem.OrderLineFields = SaveOrderLineFields();
if(productUnitId != "")
{
    cartItem.UnitId = productUnitId;
}

var newOrderLine = CartService.CreateCartLineFromModel(cart, cartItem);

if (newOrderLine != null)
{
    cart.OrderLines.Add(newOrderLine);
} 
Services.OrderLines.Save(cart.Id, cart.OrderLines);
SaveCartChanges(cart);

(I think that whoever wrote it copied it from CartController.AddLineToCart as it looks pretty similar.)

We found that with this code, no LineAdded notification is being called but I am not sure why. 

In CartService.CreateCartLineFromModel I see this
orderLine.Id = string.Empty;

Then in: CartService.NotifyOrderlinesAdded
// If the ID isn't set, then the orderline is merged - not added
if (!string.IsNullOrEmpty(newOrderLine.Id))
{
    NotifyCartLineAdded(newOrderLine, cart, pageView);
}

However, I am calling Services.OrderLines.Save(cart.Id, cart.OrderLines) which should give them an ID at that point, no?

What's the proper way to add an order line and still have LineAdded raised? We're on DW9.

Thanks!

Imar

 

 


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Hi Imar

It is the cartservice that issues the notification.

Your code calls the orderlineservice to save - orderlineservice is low-level - cartservice is higher level. Orderlineservice is also used by the backend to save an orderline - the cartservice is only frontend and related to cart commands which this is where the NotifyOrderlinesAdded is broadcast.

Call CartService.AddProducts to get the notification fired.

Or broadcast the notifcation your self:

NotificationManager.Notify(Notifications.Ecommerce.Cart.Line.Increased, new Notifications.Ecommerce.Cart.Line.IncreasedArgs(orderLine, cart, amount));

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Thanks Nicolai, that makes sense. I was a bit thrown off as the other notifications like LineIncreased do work as expected here.

I've chosen to raise my own notification as I didn't want to rewrite too much code.

Imar

 

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Lineincreased is also only coming from cartservice. So if you have an orderline and change quantity and save it using orderlineservice it will not fire the notification.

Glad you made it work!

 

You must be logged in to post in the forum