Dynamicweb 8 Documentation
SalesDiscountProvider Class
Members  Example 

The SalesDiscountProvider allows you to invent your own discount types from scratch and apply them to an order, or to extend the built-in discount types in the Sales Discount module. All SalesDiscountAddIns need to override a function called ProcessOrder, which is the function that is called when Dynamicweb eCommerce goes through all the SalesDiscountAddIns searching for discounts. This allows the whole procedure to be more flexible, allowing you to decide how your SalesDiscountAddIn should work.
Object Model
SalesDiscountProvider ClassDiscountValueHandler ClassOrderLine ClassProduct Class
Syntax
'Declaration
 
Public Class SalesDiscountProvider 
   Inherits Dynamicweb.Extensibility.ConfigurableAddIn
public class SalesDiscountProvider : Dynamicweb.Extensibility.ConfigurableAddIn 
Example
This code sample shows how to make a SalesDiscountProvider that triggers discount if the total sum of purchased products (“Order total amount”) exceeds the specified amount.
[AddInName("Total price discount")]
 [AddInDescription("If the total sum of the products purchased on an order is equal or bigger that the specified amount a discount will be triggered.") ]
 public class TotalSalesPriceDiscount : SalesDiscountProvider
{
    #region Fields
    private string _MoreLess = "";
    private double _BuyLimit = 1000.0;
    private DiscountValueHandler _DiscountValue = new DiscountValueHandler();
    #endregion
 
    #region Properties
    [AddInParameter("Condition"), AddInParameterEditor(typeof(Editors.MoreLessParameterEditor), "")]
    public string MoreLess
    {
        get { return _MoreLess; }
        set { _MoreLess = value; }
    }
 
    [AddInParameter("Order buylimit"), AddInParameterEditor(typeof(Editors.TextParameterEditor), "")]
    public double BuyLimit
    {
        get { return _BuyLimit; }
        set { _BuyLimit = value; }
    }
 
    [AddInParameter("Discount"), AddInParameterEditor(typeof(Editors.DiscountValueEditor), "")]
    public string DiscountValue
    {
        get
        {
            if (_DiscountValue == null)
                _DiscountValue = new DiscountValueHandler();
            return _DiscountValue.Value;
        }
        set
        {
            if (_DiscountValue == null)
                _DiscountValue = new DiscountValueHandler();
            _DiscountValue.Value = value;
        }
    }
    #endregion

    #region Methods
    public override void ProcessOrder(Order _order)
    {
        bool AmountValid = false;
        if (this.MoreLess == ">" && _order.PriceBeforeFees.Price > this.BuyLimit)
            AmountValid = true;
        else if (this.MoreLess == ">=" && _order.PriceBeforeFees.Price >= this.BuyLimit)
            AmountValid = true;
        else if (this.MoreLess == "==" && _order.PriceBeforeFees.Price == this.BuyLimit)
            AmountValid = true;
        else if (this.MoreLess == "<" && _order.PriceBeforeFees.Price < this.BuyLimit)
            AmountValid = true;
        else if (this.MoreLess == "<=" && _order.PriceBeforeFees.Price <= this.BuyLimit)
            AmountValid = true;
    
        if (AmountValid == true)
        {
            if (this._DiscountValue.Type == DiscountTypes.Product || this._DiscountValue.Type == DiscountTypes.Products)
            {
                foreach (Products.Product product in this._DiscountValue.Products)
                {
                    OrderLine line = new OrderLine();
                    line.OrderID = _order.ID;
                    line.Order = _order;
                    line.ProductID = product.ID;
                    line.ProductVariantID = product.VariantID;
                    line.Quantity = 1;
                    line.Modified = DateTime.Now;
                    line.ProductName = product.Name;
                    line.ProductNumber = product.Number;
                    line.Reference = "Default.aspx?ID=" + PageId + "&ProductID=" + product.ID + "&VariantID=" + product.VariantID;
                    line.Type = base.ChkString(base.ChkNumber(OrderLine.OrderLineType.Discount));
                    line.PageID = PageId;
                    line.Product = product;
                    _order.OrderLines.Add(line, false);
                }
            }
            else
            {
                // Calculate discount
                double discountPrice = 0;
                // How are we going to calculate this?
                if (this._DiscountValue.Type == DiscountTypes.Percent)
                {
                    discountPrice = (_order.PriceBeforeFees.Price / 100) * this._DiscountValue.Amount;
                }
                else if (this._DiscountValue.Type == DiscountTypes.FixedAmount)
                {
                    discountPrice = this._DiscountValue.Amount;
                }
                // Get rawprice of the discount.
                PriceRaw rawprice = new PriceRaw(discountPrice, Dynamicweb.eCommerce.Common.Application.DefaultCurrency);
                // Convert to a calculated price
                Prices.PriceCalculated CalcPrice = new Prices.PriceCalculated(rawprice);
                CalcPrice.PriceWithoutVAT = CalcPrice.PriceWithVAT;
                CalcPrice.VATPercent = 0;
                CalcPrice.VAT = 0;
                // We need a negative value
                discountPrice = CalcPrice.Price - (CalcPrice.Price * 2);
                // Add a new order line
                OrderLine line = new OrderLine();
                line.Order = _order;
                line.Quantity = 1;
                line.ProductName = this.DiscountName;
                line.SetUnitPrice(discountPrice);
                line.Type = base.ChkString(base.ChkNumber(OrderLine.OrderLineType.Discount));
                // Insert orderline
                _order.OrderLines.Add(line, false);
           }
       }
    }
    #endregion

 }
Inheritance Hierarchy

System.Object
   Dynamicweb.Extensibility.ConfigurableAddIn
      Dynamicweb.eCommerce.Orders.SalesDiscounts.SalesDiscountProvider
         Dynamicweb.eCommerce.Orders.SalesDiscounts.DateTimeDiscount
         Dynamicweb.eCommerce.Orders.SalesDiscounts.OrderFieldDiscountUniqueVoucher
         Dynamicweb.eCommerce.Orders.SalesDiscounts.ProductDiscount

Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

SalesDiscountProvider Members
Dynamicweb.eCommerce.Orders.SalesDiscounts Namespace

Send Feedback