Developer forum

Forum » Development » Product catalog for ViewModel - BeforePaging notification

Product catalog for ViewModel - BeforePaging notification

Thomas Larsen
Reply

Hi,

Would it be possible to add an BeforePaging notification on the Product catalog for ViewModel module?

Like the one on the old/normal Product catalog module

BR
Thomas


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

It could... Can you help me explain the use case? 

That notification dates way back to before razor or viewmodels. Maybe there are other and more contemporary ways to achieve it.

Thanks, Nicolai

 
Thomas Larsen
Reply

Yes of course - In this case i woul like to change the sorting of the productlist based on a custom list of product ids

In the Dynamicweb.Ecommerce.Notifications.Ecommerce.ProductList.BeforePaging notification the code looks like this

 

            if (sortOrderList != null && sortOrderList.Any())
            {
                List<Dynamicweb.Ecommerce.Products.Product> sortedProductCollection = beforePagingArgs.Products.OrderBy(p => sortOrderList.IndexOf(p.Id)).ToList();

                beforePagingArgs.Products.Clear();
                beforePagingArgs.Products.AddRange(sortedProductCollection, false);
            }
 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply
This post has been marked as an answer

I am not sure that would work.

Product catalog for viewmodel is only using the index, and we only return a list of product auto ids from the index that is in the current page. So only if you run one page with all products of a query would this approach work.

You do have a Notifications.Ecommerce.ProductCatalog.OnBeforeProductListRender notification which will give you a list of ProductAutoIds you can take a look at. But if you are paging, i.e. showing page 2 of 3 with a pagesize of 10, you only get the 10 ids for page 2.

At no point in time will the result hold the entire list for you to manipulate - the query will limit the result to what is needed.

You can also have a look at Notifications.Query.BeforeQuery - here you can change the actual query to sort by something else dynamically.

Then you have Notifications.Query.AfterQuery which will give you a IQueryResult which is the raw result of the query on the lucene index - it contains a property QueryResult which is an ienumerable of lucene documents.

What are you sorting by - and why. There might be another way...

Votes for this answer: 1
 
Thomas Larsen
Reply

ok that makes sense

I'm creating a customer most bought product list that is sorted by the quantity that the customer bought.

And I have already made a solution based on BeforeQuery notification that ignores paging (skip and take parameters). And a AfterQuery notification that does the sorting and handling paging.

But it’s not my finest code and I was looking for a cleaner approach

/Thomas

 

You must be logged in to post in the forum