Developer forum

Forum » Ecommerce - Standard features » Sortering of orderlines in basket

Sortering of orderlines in basket

Peter Bille Larsen
Reply

Hi DWs

 

One of our clients want the basket orderlines sorted in product number desc.
Is this in anyway possible as standard?

 

//Peter

 

 


Replies

 
Nicolai Høeg Pedersen
 
Peter Bille Larsen
Reply

When using sortbyfield is it always Ascending.?

 

 
Nicolai Høeg Pedersen
Reply

Yes ASC is default, but can be overridden.

 

But what I provided you is wrong, because that is on the order collection and not the orderline collection...

 

So, you would have to create a custom collection of orderlines, move them from order.orderlines, sort them, clear the order.orderlines Collection and then add them in the right order.

 

Difficult and Things could go wrong...

 

BR Nicolai

 
Peter Bille Larsen
Reply

Oki, I will recommend the client some smaller jquery solution and then order mail and print will be default.

 
Anders Ebdrup
Reply

Hi Peter,

 

It is fairly easy to rearrange the orderlines - I hope that you can use this example:

public class SortProductsExtender : OrderTemplateExtender
    {
        public override void ExtendTemplate(Template template)
        {
            if (RenderingState == TemplateExtenderRenderingState.Before)
            {
                // Load orderlines into temporary list
                List<OrderLine> tempList = new List<OrderLine>();
                foreach (OrderLine orderLine in Order.OrderLines)
                {
                    tempList.Add(orderLine);
                }

                // Reorder the orderlines
                tempList = tempList.OrderByDescending(ol => ol.ProductNumber).ToList();

                // Remove current orderlines
                Order.OrderLines.Clear();

                // Add the reordered lines to the orderlines of the order
                foreach (OrderLine item in tempList)
                {
                    Order.OrderLines.Add(item);
                }
            }
        }
    }

Best regards, Anders

 
Nicolai Høeg Pedersen
Reply

Just remember to test this if you have discounts or parts list involved. They would be suborder lines and could be messed up by sorting.

 

Nicolai

 
Peter Bille Larsen
Reply

ok, will test it with all possible aspects. Thanks for help, code and comments.

 

You must be logged in to post in the forum