Posted on 17/09/2025 22:54:58
I would say this is a classic discount. If regular price is 15, you can create a discount that triggers onm 10 productrs and give e.g. 5 dkk or 33% in discount.
You can also create priceprovider that only reacts on said product, and in that provider, see if there is a cart, and if there is return a price from e.g. a custom field. Be very careful with null checks int hat one.
You might also find some inspiration below in a custom discount using a DiscountExtenderBase and the IDiscountExtenderCalculateProductDiscount
using Dynamicweb.Core;
using Dynamicweb.Ecommerce.International;
using Dynamicweb.Ecommerce.Prices;
using Dynamicweb.Ecommerce.Products;
using Dynamicweb.Extensibility.AddIns;
using Dynamicweb.Extensibility.Editors;
using Dynamicweb.Security.UserManagement;
namespace Dynamicweb.Ecommerce.Orders.Discounts
{
public class SetAmountFromProductAsFinalPriceExtender : DiscountExtenderBase, IDiscountExtenderCalculateProductDiscount
{
[AddInParameter("Set price with discount to amount field"), AddInDescription("Will set the final discounted price of a product to the value specified on the product custom field."), AddInParameterEditor(typeof(YesNoParameterEditor), "")]
public bool CalculateDiscountToReachSpecifiedDiscountPrice { get; set; }
public PriceInfo GetDiscount(Product product, Currency currency, Country country)
{
var context = new PriceContext(currency, country, Services.Shops.GetShop(Dynamicweb.Frontend.PageView.Current()?.Area?.EcomShopId), UserContext.Current.User, Common.Context.ReverseChargeForVatEnabled, Common.Context.OrderTime);
return GetDiscount(context, product);
}
private PriceInfo GetDiscount(PriceContext context, Product product)
{
//Locating the amount set on the product in the custom field. We want to use this value as the final price after discount for this product.
double amountFromProductField = Converter.ToDouble(product.ProductFieldValues?.GetProductFieldValue(Discount.AmountProductFieldName)?.Value);
if (amountFromProductField > 0d)
{
//Load the price information located on the product in a custom field into a price calculated to handle vat etc.
var priceFromCustomField = PriceCalculated.Create(context, new PriceRaw(amountFromProductField, context.Currency));
priceFromCustomField.Calculate();
//Find the calculated price for this product
var priceFromProduct = product.GetPrice(context);
if (CalculateDiscountToReachSpecifiedDiscountPrice)
{
//calculate what the discount must be in order to achieve a product price with discount that reflects what is in the custom field on the product
var theDiscount = priceFromProduct.Substract(priceFromCustomField);
return theDiscount;
}
else
{
return priceFromCustomField;
}
}
else
{
var calculated = PriceCalculated.Create(context, new PriceRaw());
calculated.Calculate();
return calculated;
}
}
public override bool DiscountValidForProduct(Product product)
{
//This discount needs a field name specified in order to work
if (string.IsNullOrEmpty(Discount.AmountProductFieldName))
{
return false;
}
//This discount only works if the field has a positive value
double fieldValue = Converter.ToDouble(product.ProductFieldValues?.GetProductFieldValue(Discount.AmountProductFieldName)?.Value);
return fieldValue > 0.0d;
}
public override bool DiscountValidForOrder(Order order)
{
//this discount type only applies for products
return false;
}
}
}