// find the current tier pricing of the product
var tierProductPrice = product.ProductPrice.FirstOrDefault(p => p.MinQty >= qtyInCart && p.MaxQty <= qtyInCart);
if (tierProductPrice != null)
{
var maxTierQty = tierProductPrice.MaxQty;
var tierPrice = tierProductPrice.Price;
// current price of the Cart
var cartPrice = tierPrice * qtyInCart;
// find next min price Tier
var nextTierProductPrice = product.ProductPrice.FirstOrDefault(p => p.MinQty >= maxTierQty && p.MaxQty <= maxTierQty);
if (nextTierProductPrice != null)
{
var itemsToAdd = nextTierProductPrice.MinQty - qtyInCart;
// calculate new price of the cart
var newPrice = nextTierProductPrice.MinQty * nextTierProductPrice.Price;
if (newPrice < cartPrice)
{
productSaving = new ProductSaving
{
QtyToAdd = itemsToAdd,
Savings = cartPrice - newPrice
};
}
}
}
note that the above logic assumes that the immediate next tier is the only tier that is going to be profitable for the user for adding enough items to reach that tier. from the data it looks like it.
i.e. if i have 18 items currently in my cart, only the 20-99 tier may possibly result in a better deal. if 20-99 could not yield a better deal, then the tiers above 99 are never going to yield a better deal.
let us know if that assumption is not right. the code logic to determine nextTierProductPrice will go into a loop till we hit a better deal OR deplete all tiers.
i haven't put the logic unnecessarily until needed.