Developer forum

Forum » Ecommerce - Standard features » Sorting on a custom price field

Sorting on a custom price field

Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi there,

I am trying to sort products based on the price from the price matrix. Here's what I did:

1. Added a new field to the index called PriceDkk and mapped it to the field ProductPrice|DKK|DK|WithVAT (which is autogenerated from the matrix)
2. Rebuilt the index
3. Updated Designs/Swift/Paragraph/ProductListFacets/Sorting.cshtml to use PriceDkk instead of Price when sorting by price.
4. Enabled the price filter in the Filters paragraph. 

Now when I choose Sort by Price Low/High, the query string is updated with SortBy=PriceDkk or SortBy=-PriceDkk

However, the results are in classical DynamicwWeb style: not what you'd expect.

Sort by PriceDkk results in this:

DKK 15,00
DKK 31,25
DKK 15,00
DKK 18,75
DKK 31,25
DKK 28,98

Sort by -PriceDkk results in this:

DKK 28,98
DKK 31,25
DKK 18,75
DKK 15,00
DKK 31,25
DKK 15,00

So reverting the sort direction reverses the results, but neither list is sorted.

In case it matters: one of the products on the page does not have a price in the matrix. One of the products is a variant with two options each having the same price.

What am I missing?

Imar

 


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

I don't know :-). Configuration mistake maybe?

Tried making the same setup here:

https://swiftdemo.dynamicweb-cms.com/e-bikes/classic-e-bikes?PageSize=12&RequestType=UpdateList&SortBy=PriceDkk
https://swiftdemo.dynamicweb-cms.com/e-bikes/classic-e-bikes?PageSize=12&RequestType=UpdateList&SortBy=-PriceDkk

And on more products:

https://swiftdemo.dynamicweb-cms.com/products?OriginalPageSize=12&SortBy=PriceDkk&RequestType=UpdateList&ParagraphID=7833&PageSize=24

In this last link, you can see that the first product have prices in the range 73,78 kr. - 80,92 kr. which is more than the second product in the list. And that is because it is a master product with price 0 and a number of variants that has prices. So the index will sort the master up as the first entry as that document has price = 0 DKK, but the variants have different prices and therefore displays what looks like an error.

Made the field like this:

Indexed, not analyzed, double:

BR Nicolai

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Are your prices standard ones or from the price matrix? I checked a bunch of products on your solution and all of them had just a price on the product card and nothing in the price matrix. In my case, all prices on the product card are 0 and they have their price in the price matrix instead. Could that make a difference?

Imar

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply
This post has been marked as an answer

Also works with price matrix:

https://swiftdemo.dynamicweb-cms.com/products?OriginalPageSize=12&SortBy=PriceDkk&RequestType=UpdateList&ParagraphID=7833&PageSize=24&currencycode=dkk

Gives this product:

The

The price in DKK:

And the price on the product card:

Show me your entire price record for your products - are they tied to i.e. a user? Because user specific prices or other context related stuff is not taken into consideration.

Or share a link to the solution.

Votes for this answer: 1
 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Oh, your "or other context related stuff" note got me somewhere. I was wondering if it had to do with the start and end dates of the prices, so I looked into ProductIndexBuilder and found this:

var allPrices = Prices.Price.GetPricesBySql("SELECT * FROM EcomPrices WHERE PriceIsInformative <> 1");

Turns out my EcomPrices table had null, not 0 for the PriceIsInformative column (they are imported from integration and this column is unmapped). <> 1 evaluates values and therefore skipped my null records, leading to no prices in the index. Once I updated them all to 0 (and updated the mapping for future prices) things started to work.

Is the <> 1 intentional? Does ProductIndexBuilder mean to include prices where PriceIsInformative  IS NULL, or only where it has an explicit value of 0?

Thanks!

Imar

 

You must be logged in to post in the forum