Table of Contents

Interface IDiscountExtenderCalculateProductDiscount

Namespace
Dynamicweb.Ecommerce.Orders.Discounts
Assembly
Dynamicweb.Ecommerce.dll

Implement this interface on a DiscountExtenderBase implementation to take over the calculation of a discount for a product

public interface IDiscountExtenderCalculateProductDiscount

Examples

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 priceContext = new PriceContext(currency, country);
                    var priceFromProduct = product.GetPrice(priceContext);

                    //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

If this interface is implemented it takes over discount calculation for a product.

Methods

GetDiscount(Product, Currency, Country)

Return the discount amount as a priceinfo that should be given to the passed product

PriceInfo GetDiscount(Product product, Currency currency, Country country)

Parameters

product Product

The product to calculate the discount for

currency Currency
country Country

Returns

PriceInfo

The amount that is wanted to be given in discount

To top