Developer forum

Forum » Development » Orderlines sorting

Orderlines sorting


Reply
How do i change the sorting of the orderlines i Cart V2. I want the newest added lines in top?

Replies

 
Reply

Hi there,

I haven't fully tested the solution below, but I think you can accomplish this with an OrderTemplateExtender that grabs the lines, sorts them and reassigns them to the order. The following should work:

public class SortProductsExtender : OrderTemplateExtender
{
  public override void ExtendTemplate(Template template, TemplateExtenderRenderingState renderingState)
  {
    if (renderingState == TemplateExtenderRenderingState.Before)
    {
      List<OrderLine> tempList = new List<OrderLine>();
      foreach (OrderLine orderLine in Order.OrderLines)
      {
        tempList.Add(orderLine);
      }

      tempList = tempList.OrderByDescending(o => o.ID).ToList();

      Order.OrderLines.Clear();
      foreach (OrderLine item in tempList)
      {
        Order.OrderLines.Add(item);
      }
    }
  }
}

You need at least references to System.Linq and System.Collections.Generic to make this work.

Hope this helps,

Imar

 
Reply
Nice:) You may need to pay special attention to sales discount lines since they are simply order lines (TypeID=3) sorted final in the order lines collection.

BR.
Lars
 
Reply
Good point.... In that case, you can replace the last for each block with this code that adds the lines in two groups: products and discounts:

foreach (OrderLine item in tempList.Where(ol => ol.Type != "3"))
{
 Order.OrderLines.Add(item);
}
foreach (OrderLine item in tempList.Where(ol => ol.Type == "3"))
{
 Order.OrderLines.Add(item);
}

Cheers,

Imar
 
Reply
Thanks Imar,
 
I will try this.

Regards
Thomas

 

You must be logged in to post in the forum