Table of Contents

Class DiscountExtenderBase

Namespace
Dynamicweb.Ecommerce.Orders.Discounts
Assembly
Dynamicweb.Ecommerce.dll
Represents an extender that can be added to a discount (discount matrix) as additional custom condition
public abstract class DiscountExtenderBase : ConfigurableAddIn
Inheritance
DiscountExtenderBase
Inherited Members

Examples

using System;
using Dynamicweb.Ecommerce.Products;
using Dynamicweb.Ecommerce.Orders;
using Dynamicweb.Ecommerce.Orders.Discounts;
using Dynamicweb.Extensibility.AddIns;
using Dynamicweb.Extensibility.Editors;
using System.Collections;

namespace Dynamicweb.Ecommerce.Examples.Orders.Discounts
{
    /// <summary>
    /// The example of an extender which can be used with product and order discounts - if applied the discount will be valid on the specified day only
    /// </summary>
    public class SpecificDayDiscountExtender : DiscountExtenderBase, IDropDownOptions
    {
        [AddInParameter("Favorite day"), AddInDescription("Specify which day when this discount applies"), AddInParameterEditor(typeof(DropDownParameterEditor), "")]
        public string FavoriteDay { get; set; }

        /// <summary>
        /// Constructor - sets the favorite day to Tuesday
        /// </summary>
        public SpecificDayDiscountExtender()
        {
            FavoriteDay = DayOfWeek.Tuesday.ToString();
        }

        /// <summary>
        /// If this extender is used for a product discount, it will be checked here
        /// </summary>
        public override bool DiscountValidForProduct(Product product)
        {
            return CheckConditios();
        }

        /// <summary>
        /// If this extender is used for an order discount, it will be checked here
        /// </summary>
        public override bool DiscountValidForOrder(Order order)
        {
            return CheckConditios();
        }

        /// <summary>
        /// Checks if the conditions for this discount are valid. In this case it checks if todays day is equal to the specified favorite day where the discount applies
        /// </summary>
        private bool CheckConditios()
        {
            DayOfWeek day;
            if (Enum.TryParse(FavoriteDay, out day))
            {
                return DateTime.Now.DayOfWeek == day;
            }
            return false;
        }

        /// <summary>
        /// Return options for the favorite day dropdown in the user interface
        /// </summary>
        Hashtable IDropDownOptions.GetOptions(string dropdownName)
        {
            switch (dropdownName)
            {
                case "Favorite day":
                    var list = new Hashtable
                    {
                        { DayOfWeek.Monday, DayOfWeek.Monday.ToString() },
                        { DayOfWeek.Tuesday, DayOfWeek.Tuesday.ToString() },
                        { DayOfWeek.Wednesday, DayOfWeek.Wednesday.ToString() },
                        { DayOfWeek.Thursday, DayOfWeek.Thursday.ToString() },
                        { DayOfWeek.Friday, DayOfWeek.Friday.ToString() },
                        { DayOfWeek.Saturday, DayOfWeek.Saturday.ToString() },
                        { DayOfWeek.Sunday, DayOfWeek.Sunday.ToString() }
                    };
                    return list;
                default:
                    throw new ArgumentException($"Unknown dropdown name: '{dropdownName}'");
            }
        }
    }
}
using Dynamicweb.Core;
using Dynamicweb.Ecommerce.International;
using Dynamicweb.Ecommerce.Prices;
using Dynamicweb.Ecommerce.Products;
using Dynamicweb.Extensibility.AddIns;
using Dynamicweb.Extensibility.Editors;

namespace Dynamicweb.Ecommerce.Orders.Discounts
{
    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;
        }
    }
}

Remarks

You should either override DiscountValidForProduct to use the extender in product discounts, or override DiscountValidForOrder to use the extender for order discounts. To use the extender for both kind of discounts you must override both methods.

Properties

Discount

Gets the discount that this extender has been added to
public Discount Discount { get; }

Property Value

Discount

Methods

DiscountValidForOrder(Order)

Determines if discount is valid for given order.
public virtual bool DiscountValidForOrder(Order order)

Parameters

order Order
The order to conduct the check for.

Returns

bool
true if discount is valid for the specified order; otherwise, false.

Examples

using Dynamicweb.Ecommerce.Orders;
using Dynamicweb.Ecommerce.Orders.Discounts;
using Dynamicweb.Extensibility.AddIns;
using Dynamicweb.Extensibility.Editors;

namespace Dynamicweb.Ecommerce.Examples.Orders.Discounts
{
    /// <summary>
    /// The example of an extender for order discounts
    /// </summary>
    public class FavoriteNameDiscountExtender : DiscountExtenderBase
    {
        [AddInParameter("Favorite name"), AddInDescription("Specify what a customer name should start with to get this discount"), AddInParameterEditor(typeof(TextParameterEditor), "")]
        public string FavoriteName { get; set; }

        /// <summary>
        /// Constructor - sets the favorite name to Marta
        /// </summary>
        public FavoriteNameDiscountExtender()
        {
            FavoriteName = "Marta";
        }

        /// <summary>
        /// If this extender is used for an order discount, it will be checked here
        /// Checks if the name on the order starts with the specified name and returns true to apply the discount
        /// </summary>
        public override bool DiscountValidForOrder(Order order)
        {
            var valid = false;

            if (!string.IsNullOrEmpty(FavoriteName) && !string.IsNullOrEmpty(order.CustomerName))
            {
                valid = order.CustomerName.StartsWith(FavoriteName, System.StringComparison.OrdinalIgnoreCase);
            }

            return valid;
        }


    }
}

DiscountValidForProduct(Product)

Determines if discount is valid for given product.
public virtual bool DiscountValidForProduct(Product product)

Parameters

product Product
The product to conduct the check for.

Returns

bool
true if discount is valid for the specified product; otherwise, false.

Examples

using Dynamicweb.Ecommerce.Products;
using Dynamicweb.Ecommerce.Orders.Discounts;
using Dynamicweb.Extensibility.AddIns;
using Dynamicweb.Extensibility.Editors;

namespace Dynamicweb.Ecommerce.Examples.Orders.Discounts
{
    /// <summary>
    /// The example of an extender for product discounts - if applied the discount will be valid for products with a price less than specified
    /// </summary>
    public class CheapProductsDiscountExtender : DiscountExtenderBase
    {

        [AddInParameter("Cheap price"), AddInDescription("Specify max price a product should have to get the discount"), AddInParameterEditor(typeof(IntegerNumberParameterEditor), "minValue=0;maxValue=1000;allowNegativeValues=false;localizeValues=true;")]
        public int CheapPrice { get; set; }

        /// <summary>
        /// Constructor - sets the maximum price to 100
        /// </summary>
        public CheapProductsDiscountExtender()
        {
            CheapPrice = 100;
        }

        /// <summary>
        /// If this extender is used for a product discount, it will be checked here
        /// </summary>
        public override bool DiscountValidForProduct(Product product)
        {
            return product.DefaultPrice <= CheapPrice;
        }

    }
}
To top