Developer forum

Forum » Development » Productcollection Sort by price doesnt work

Productcollection Sort by price doesnt work

Jonas Kæseler
Reply

Hello,

 

I have this subscriber that sorts products by price:

    [Subscribe(Dynamicweb.Notifications.eCommerce.ProductList.BeforePaging)]
    public class ChangeSorting : NotificationSubscriber
    {

        public override void OnNotify(string notification, Dynamicweb.Extensibility.NotificationArgs args)
        {
            Dynamicweb.Notifications.eCommerce.ProductList.BeforePagingArgs thisArgs = (Dynamicweb.Notifications.eCommerce.ProductList.BeforePagingArgs)args;

            thisArgs.Products.Sort(Dynamicweb.eCommerce.Products.ProductCollection.SortBy.Price,Dynamicweb.eCommerce.Products.ProductCollection.SortDirection.Ascending);

            foreach (Dynamicweb.eCommerce.Products.Product p in thisArgs.Products)
            {
               LogToFile.Log(p.Number + " " + p.Price.PriceFormatted, "SortLog", LogToFile.LogType.ManyEntriesPerFile);
            }
        }
    }

 

Unfortunately this doesnt seem to work very well. Here's an excerpt from the SortLog:

[2/13/2013 9:00:01 AM]:	84717 kr. 173,75
[2/13/2013 9:00:01 AM]:	84326 kr. 173,75
[2/13/2013 9:00:01 AM]:	84104 kr. 173,75
[2/13/2013 9:00:01 AM]:	84103X103 kr. 198,75
[2/13/2013 9:00:01 AM]:	84348X348 kr. 173,75
[2/13/2013 9:00:01 AM]:	84795X795 kr. 186,25
[2/13/2013 9:00:01 AM]:	84613 kr. 173,75
[2/13/2013 9:00:01 AM]:	84288X288 kr. 173,75
[2/13/2013 9:00:01 AM]:	84959 kr. 173,75

 

Note: I also have a priceprovider setting the prices before all this, but shouldn't this still work ?

 

Cheers,

Jonas

 


Replies

 
Morten Bengtson
Reply

Hi Jonas,

SortBy.Price will sort the products by Product.DefaultPrice and not the price calculated by price providers.

 

 
Morten Bengtson
Reply

As you can also see on Dynamicwebs own solutionset, sorting on default price and displaying the calculated price is not a good idea:

http://solutionset.dynamicweb.dk/en-US/ProductList/Bikes/Mountain-bikes.aspx?SortBy=Price&SortOrder=ASC

 

 
Jonas Kæseler
Reply

Thanks Morten for pointing that out so clearly.

 

Do you know if this is intentional default behaviour? It seems a little irrational to me.

 

So.. I need to sort it manually. I'll have a look at it.

 

Cheers,

Jonas

 
Jonas Kæseler
Reply

Oh, setting the default price on every product before sorting apparently will do it:

                    foreach (Dynamicweb.eCommerce.Products.Product p in thisArgs.Products)
                    {
                        p.DefaultPrice = p.Price.Price;
                    }
                    thisArgs.Products.Sort(Dynamicweb.eCommerce.Products.ProductCollection.SortBy.Price, Dynamicweb.eCommerce.Products.ProductCollection.SortDirection.Ascending);

 

 
Allan Iversen
Reply

Any solution for this? I got the exactly same issue at this given moment.

Dynamicweb?

- Allan

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Hi Allan,

 

There is a limitation in the Product Catalog when it comes to prices. By default, the price sorting looks at DefaultPrice. This is because that information is retrieved with the rest of the Product information, which makes this a very inexpensive operation. If we were to sort by a custom price (PriceProvider, Price Matrix or other), we would have to request the price information for all products in the current view (filter, group selection or search) and perform paging afterwards. This could potentially be very expensive, which is why we have not made a general implementation (yet). We have, however, given you as the developer the tools to do this yourself because you know the best solution for your exact challenge.

 

There are multiple ways to solve this problem. Here's one:

 

Implement an ProductList.BeforePaging notification subscriber. This gives you the option to completely control the sorting in any way you want. If you want to sort by a price from eCommerce (ie. not a PriceProvider), you might consider invoking args.Products.LoadPrices() before sorting.

 

This topic is also discussed in this thread including code sample and how this interacts with Optimized Product Retrieval, although the origin there was sorting by a custom field: http://developer.dynamicweb-cms.com/forum/development/how-to-sort-products-by-custom-product-field.aspx

 

- Jeppe

 

You must be logged in to post in the forum