Developer forum

Forum » Development » Sort prices

Reply
Hi.

How do I make sure that prices will come out in the same order that they are displayed in the ecom.
When I iterate through a products prices like this:
foreach (Price price in product.Prices)
{
    var option = product.VariantGroups.FindOptionByID(price.VariantID);
    pricesLoop.SetTag("Option.Name", option.Name);
    pricesLoop.SetTag("Price.AmountAsNumber", price.Amount);
}

I can create a large matrix of prices which almost works like a charm. Problem is that there are minor specific deviations where the prices has switched places:

01 apr - 31 jul 2010 715{dbl} 1.425{sgl} 600{trpl} Gratis{Barn A} 200{Barn B}
01 aug - 31 aug 2010 200{Barn B} 930{dbl} 1.860{sgl} 765{trpl} Gratis{Barn A}
01 sep - 19 dec 2010 715{dbl} 1.425{sgl} 600{trpl} Gratis{Barn A} 200{Barn B}
20 dec - 06 jan 2011 930{dbl} 1.860{sgl} 765{trpl} Gratis{Barn A} 200{Barn B}
07 jan - 31 mar 2011 715{dbl} 1.425{sgl} 600{trpl} Gratis{Barn A} 200{Barn B}
01 aug - 31 aug 2010 930{dbl} 1.860{sgl} 765{trpl} Gratis{Barn A} 200{Barn B}

The value in {} is the variant option connected to the price. as you can see in 01 aug - 31 aug 2010 it has "taken a step to the right".

Any ideas how I can make sure that the sort order will always be the same?

best regards Sune Fengel

Replies

 
Reply
Hi Sune

It seems that prices are only sorted on quantity and not variant id.
You will propably need to create a List<Price> with the prices and sort that list using a IComparer<Price>.

The code will look something like this:

List<Price> myPrices = new List<Price>(product.Prices);
myPrices.Sort(new PriceComparer());
foreach (Price price in myPrices) {
...
}


public class PriceComparer : IComparer<Price> {
    public double Comparer(x, y) {
        // Return whatever makes the prices sort the way you want, e.g. String.Compare(x.VariantID, y.VariantID)
    }
}

 - Lasse

 

You must be logged in to post in the forum