Posted on 11/03/2016 10:12:44
This is my code. I inherit from ProductDiscount, but copied the fields to get a proper sorting on them and my custom field. When you save a SalesDiscount where you have selected this, it sets "SalesDiscountDiscountType" to "NoZebra.S_DW_FeenOgFroen.CustomCode.NZTotalSalesPriceDiscount", and as stated previously, the GetAllProductDiscounts method only looks for "Dynamicweb.eCommerce.Orders.SalesDiscounts.ProductDiscount".
[AddInName("NZProduct discount")]
[AddInIsAdditional(false)]
[AddInDescription("The product specific discount lets you apply a discount to a certain product. E.g. a customer buys product X and gets a 10 percent discount on that product.")]
[AddInOrder(1)]
[AddInActive(true)]
public class NZProductDiscount : ProductDiscount
{
private ProductsAndGroupsHandler _ProductsAndGroups;
[AddInParameter("Products")]
[AddInParameterEditor(typeof(ProductsAndGroupsEditor), "width=300px;height=100px")]
public string ProductsAndGroups
{
get
{
if (this._ProductsAndGroups == null)
this._ProductsAndGroups = new ProductsAndGroupsHandler();
return this._ProductsAndGroups.ToString();
}
set
{
if (this._ProductsAndGroups == null)
this._ProductsAndGroups = new ProductsAndGroupsHandler();
this._ProductsAndGroups.ParseData(value, ProductsAndGroupsHandler.Types.All);
}
}
[AddInParameterEditor(typeof(YesNoParameterEditor), "")]
[AddInParameter("Don't apply to already discounted products")]
public bool NotAlreadyDiscounted { get; set; }
public NZProductDiscount()
{
this._ProductsAndGroups = new ProductsAndGroupsHandler();
}
public override void ProcessOrder(Order order)
{
List<OrderLine> orderLinesForDiscount = NotAlreadyDiscounted ? DiscountHelper.GetLinesWithoutDiscount(order) : order.ProductOrderLines.ToList();
switch (this.DiscountValue.Type)
{
case DiscountTypes.FixedAmount:
OrderLine amountOrderline1 = DiscountHelper.CreateAmountOrderline(order, this.DiscountValue.Amount, false, null, this.IsProductDiscount, this.ProductDiscountProduct, this.DiscountName, this.DiscountID, orderLinesForDiscount);
order.OrderLines.Add(amountOrderline1, false);
break;
case DiscountTypes.Percent:
OrderLine amountOrderline2 = DiscountHelper.CreateAmountOrderline(order, this.DiscountValue.Amount, true, null, this.IsProductDiscount, this.ProductDiscountProduct, this.DiscountName, this.DiscountID, orderLinesForDiscount);
order.OrderLines.Add(amountOrderline2, false);
break;
case DiscountTypes.Products:
foreach (Product product in this.DiscountValue.ProductCollection)
{
order.OrderLines.Add(DiscountHelper.CreateProductOrderline(order, product, this.IsProductDiscount, this.ProductDiscountProduct, this.PageId, this.DiscountID), false);
}
break;
}
}
}