Posted on 03/12/2021 15:19:27
It is not supported no.
You need to create a DiscountExtenderBase and add either IDiscountExtenderCalculateProductDiscount or IDiscountExtenderCalculateOrderDiscount
Since this is something that relates to an order and not the product it self, you should probably do an order discount and use IDiscountExtenderCalculateOrderDiscount
Here an example using product discount - but the idea is the same
public class SetAmountFromProductAsFinalPriceExtender : DiscountExtenderBase, IDiscountExtenderCalculateProductDiscount
{
[AddInParameter("Calculate discount based on 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)
{
//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 > 0)
{
//Load the price information located on the product in a custom field into a price calculated to handle vat etc.
var priceFromCustomField = PriceCalculated.Create(currency, country, new PriceRaw(amountFromProductField, currency));
priceFromCustomField.Calculate();
if (CalculateDiscountToReachSpecifiedDiscountPrice)
{
//Find the calculated price for this product
var priceFromProduct = product.GetPrice(currency.Code, country.Code2);
//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(currency, country, new PriceRaw(0d, currency));
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);
if (fieldValue > 0) { return true; }
return false;
}
public override bool DiscountValidForOrder(Order order)
{
//this discount type only applies for products
return false;
}
}
}