Developer forum

Forum » Development » Removing OrderLines in code

Removing OrderLines in code

Arnór Halldórsson
Arnór Halldórsson
Reply

Sup fellow nerds,

Is there a better way to completely remove all orderlines from an order (in the db as well) through code than this:

            List<Dynamicweb.Ecommerce.Orders.OrderLine> orderLines = dwOrder.OrderLines.ToList();
            foreach (Dynamicweb.Ecommerce.Orders.OrderLine orderLine in orderLines)
            {
                dwOrder.OrderLines.Remove(orderLine);
                Services.OrderLines.Delete(orderLine.Id);
            }

At first I tried this:

dwOrder.OrderLines.Clear();
Services.Orders.Save(dwOrder);

That would remove the OrderLines from the Order object, but they still existed in the EcomOrderLines table, and after each recycle of the web the orderlines got replicated, so if I started out with 5 lines and recycled, I had 10, then 15, 20... :)

Would be nice if the OrderLine or Order services included a method to yeet all orderlines at once.

Best regards,
Arnór


Replies

 
Nicolai Pedersen
Reply

Hi Arnor

'sup!

Services.Orders.Save(dwOrder); will only save changed and add new orderlines. It does not take care of removes in the database. The collection.clear is just a memory operation and does not get persisted either.

So your second approach is the only way I can see...

BR Nicolai

 
Arnór Halldórsson
Arnór Halldórsson
Reply

Hi Nicolai,

Gotcha.

Wouldn't it make sense though to have a DeleteAll (or YeetAll for the millenials) method on the OrderLineService? :)

Br, Nóri

 
Nicolai Pedersen
Reply
This post has been marked as an answer

I have just made a DeleteAll that takes an orderid on the orderline service.

Devops#2747

Coming to a Dynamicweb.Ecommerce package near you with the next release of that package!

BR Nicolai

Votes for this answer: 1
 
Arnór Halldórsson
Arnór Halldórsson
Reply

My man! :D

Tusind tak!

 

You must be logged in to post in the forum