Posted on 07/05/2026 14:35:33
Hi,
I want to get PriceBeforeDiscount for the variants of a product and find the minimum before-discount price to display on the product list page (to illustrate the price comparison).
I wrote the below function to iterate through the product variants and retrieve the PriceBeforeDiscount object for variant. However, all the price values are coming as 0 (please refer to the screenshot).
Any help would be greatly appreciated.
@functions {
public string GetMinVariantBeforePrice(
Dynamicweb.Ecommerce.ProductCatalog.ProductViewModel product,
Dynamicweb.Ecommerce.ProductCatalog.ProductViewModelSettings settings,
string unitId = "",
bool usePriceWithoutVat = false)
{
string beforePriceMin = "";
double lowestPriceValue = double.MaxValue;
bool hasVariants = product?.VariantInfo?.VariantInfo != null && product.VariantInfo.VariantInfo.Any();
if (hasVariants)
{
foreach (var variantNode in product.VariantInfo.VariantInfo)
{
// Generate the ViewModel for variant
var variantViewModel = ViewModelFactory.CreateView(settings, product.Id, variantNode.VariantID);
if (variantViewModel != null)
{
// Screenshot is for variantViewModel.PriceBeforeDiscount object in below line
var currentBeforePriceObj = !string.IsNullOrEmpty(unitId)? variantViewModel.GetPrice(unitId)?.PriceBeforeDiscount: variantViewModel.PriceBeforeDiscount;
if (currentBeforePriceObj != null && currentBeforePriceObj.Price > 0 && currentBeforePriceObj.Price < lowestPriceValue)
{
lowestPriceValue = currentBeforePriceObj.Price;
beforePriceMin = usePriceWithoutVat? currentBeforePriceObj.PriceWithoutVatFormatted: currentBeforePriceObj.PriceFormatted;
}
}
}
}
// If no variants had a valid discount (so lowestPriceValue is still MaxValue), use fallback
if (lowestPriceValue == double.MaxValue)
{
var fallback = !string.IsNullOrEmpty(unitId)? product?.GetPrice(unitId)?.PriceBeforeDiscount: product?.PriceBeforeDiscount;
// Safety check here as well to ensure the fallback master product price is also > 0
if (fallback != null && fallback.Price > 0)
{
beforePriceMin = usePriceWithoutVat ? fallback.PriceWithoutVatFormatted : fallback.PriceFormatted;
}
}
return beforePriceMin ?? "";
}
}
Following parameters passed to above fucntion.
var product = (ProductViewModel)Dynamicweb.Context.Current.Items["ProductDetails"];
var unitId = "";
var showWithoutVat = true;
var viewSettings = new Dynamicweb.Ecommerce.ProductCatalog.ProductViewModelSettings
{
LanguageId = product?.LanguageId ?? Dynamicweb.Ecommerce.Common.Context.LanguageID
};
viewSettings.FilledProperties = new[] { "Price", "PriceInformative", "PriceBeforeDiscount", "Discount", "ProductDiscounts", "Prices" };
string beforePrice = GetMinVariantBeforePrice(product, viewSettings, unitId, showWithoutVat);
